Saturday 30 April 2022

Question : - Describe the usage of any four methods in string class in Java. जावा में स्ट्रिंग क्‍लास के किन्‍हीं चार मैथडों के बारे में बताइए।


Method

Description

Return Type

length()

returns the length of a specified string.

दिए गए शब्‍द या वाक्‍य की लेंथ बताता है।

 Example:-

    String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    System.out.println(txt.length()); //26

String

toUpperCase()

Converts a string to upper case letters

शब्‍द या वाक्‍य के स्‍माल लेटर्स को कैपीटल लेटर में बदल देता है।

Example:-

        String s="Sachin";    

        System.out.println(s.toUpperCase());//SACHIN    

String

trim()

Removes whitespace from both ends of a string

यह शब्‍द या वाक्‍य में से आगे और पीछे गैरजरूरी स्‍पेस को हटा देता है।

 Example:-

    String myStr = "       Hello World!        ";

    System.out.println(myStr); //             Hello World!

    System.out.println(myStr.trim());//Hello World!

String

concat()

appends (concatenate) a string to the end of another string.

दो शब्‍दों या वाक्‍यों को जोडने के लिए प्रयोग किया जाता है।

 Example:-

    String firstName = "John ";

    String lastName = "Doe";

    System.out.println(firstName.concat(lastName)); // John Doe

String


Question:- Explain the usage of public, private and protected in java. जावा में पब्लिक, प्राइवेट व प्रोटेक्‍टेड के उपयोग का विवरण बताइये।


 

1.  The public Access Modifier (ठीक पब्लिक टॉयलेट की भांति)

  The public access modifier specifies that class variables and methods are accessible to anyone, both inside and outside the class. This means that public class members have global visibility and can be accessed by any other object. Some examples of public member variables follow:


पब्लिक कीवर्ड क्‍लास के वेरिएवल एवं मेथड को सार्वजनिक तौर पर प्रयोग करने की इजाजत प्रदान करता है। अर्थात् जिस क्‍लास को पब्लिक घोषित कर दिया जाता है उसे कोई भी ऑब्‍जेक्‍ट प्रयोग कर सकता है। उदाहरण

 

public int count;

public boolean isActive;

 

2.  The protected Access Modifier (ठीक घर के कॉमन टॉयलेट की भांति)

   The protected access modifier specifies that class members are accessible only to methods in that class and subclasses of that class. This means that protected class members have visibility limited to subclasses. Examples of a protected variable and a protected method follow:

प्रोटेक्‍टेड कीवर्ड वेरिएवल एवं मेथडस को खुद की क्‍लास के साथ साथ केवल अपनी चाइल्‍ड क्‍लास को भी प्रयोग करने की इजाजत प्रदान करता है। अर्थात जिन वेरिएवल एवं मैथड को प्रोटेक्‍टेड घोषित कर दिया जाता है वे केवल उसी के बच्‍चों को प्रयोग करने के लिए दिखाई देते हैं। उदाहरण

 

protected char middleInitial;

protected char getMiddleInitial() 

{

return middleInitial;

}

 

3. The private Access Modifier (ठीक रूम के अटैच टॉयलेट की भांति)

    The private access modifier is the most restrictive; it specifies that class members are accessible only by the class in which they are defined. This means that no other class has access to private class members, even subclasses. Some examples of private member variables follow:

प्राइवेट कीवर्ड वेरिएवल एवं मेथड्स को केवल खुद की क्‍लास भीतर ही प्रयोग करने की इजाजत प्रदान करता है। अर्थात जिन वेरिएवल एवं मैथड को प्राइवेट घोषित कर दिया जाता है वे केवल उसी क्‍लास में प्रयोग की जा सकती है यहां तक कि उनके बच्‍चों(चाइल्‍ड क्‍लास) को भी प्रयोग करने के लिए उपलब्‍ध नहीं होती है। उदाहरण

 

 

private String firstName;

private double howBigIsIt;