Skip to main content

Strings In Java

Question: What is String in JAVA ?
Ans: Java me, "String" ek data type hota hai jo text ko represent karta hai. String ek sequence of characters hoti hai, jaise ki alphabets, numbers, symbols, aur special characters. Java me, String class predefined hoti hai, aur aap iska istemal karke text data ko store, manipulate, aur access kar sakte hain.

Strings are non Primitive Data Type.

Question: What is .length(); ?
Ans: this is to check length of a String .

Question: Different between single quote and double quote in String .
Ans: Single quote ' ' ['D'] is basically used to store character but double quote is used to store " " ["Divyanshu"] a string basically sequence of character . 


Question: How to add, concat a string ?
Ans : There is two way to concat or add a string
            1) first is to use 
Addition (+) arithemetic operator
            2) .concat




package learningJava;

import java.util.*;

public class regPract {

/* Find the maximum and minimum element in an array

*/

public static void main(String[] args) {

String str1 = ("Digital");

String str2 = ("India");

*\this is method of java concat is a                         method of java two concatenate string*/

String str = str1.concat(str2);

System.out.println(str);

//this is general option to add a string

System.out.println(str1+str2);

}

}







Output:
DigitalIndia

DigitalIndia

Question: What is .equals in String ?
Ans: str1.equals(str2); This is for check 2 strings are qual or not and it will give you boolean result like true and false

Question: What is .compareTo method in java ?
Ans: 
String str1 = "apple";

String str2 = "banana"; int result = str1.compareTo(str2); if (result < 0) { System.out.println("str1 is smaller than str2"); } else if (result > 0) { System.out.println("str1 is larger than str2"); } else { System.out.println("str1 is equal to str2"); 

} 


This will compare two strings 


Question: What is equalsIgnoreCase is java ?

Ans: The equalsIgnoreCase method is a Java String class method used to compare two strings for equality while ignoring the differences in their letter case (uppercase or lowercase). It checks if two strings have the same characters, regardless of whether the characters are in uppercase or lowercase.

String is a class


Question: How many types of class available on java to create String class ?
Ans: There're 3 classes available here to create a String class

1) String
2) String buffer
3) String builder

Question: How to use concat is JAVA ?
Ans: 


package learningJava;

import java.util.*;

public class regPract {

/* Find the maximum and minimum element in an array

*/

public static void main(String[] args) {

String str = "Divyanshu";

str = str.concat("khare");

System.out.println(str);

}

}

Question: contains what is this ?





Comments

Popular posts from this blog

Polity

  1.    सन 1600 में ईस्ट इंडिया कंपनी भारत आई थी जिसका परमिशन ब्रिटिश की महारानी एलीजाबेथ ने दिया था 2.    परमिशन में चार्टर दिया गया था साथ ही मोनोपोली दी गयी थी अलीजाबेत के द्वारा 3.    बिटिश ईष्ट इंडिया कंपनी भारत शिप से आई थी जिस शिप का नाम था रेड ड्रैगन 4.    भारत में आने के बाद उन्होंने पहली फैक्ट्री 1611 मछलीपटनम में बनाई 5.    दूसरी फैक्ट्री 1612 में सूरत में बनाया था 6.    फैक्ट्री नियन्त्र के लिए तीन प्रेसीडेंसी बनायीं गयी जो थी बॉम्बे, बंगाल, मद्रास 7.    बंगाल का राजा था सिराजुदुल्ला और ब्रिटिश रोबर्ट clive युद्ध किया 1757 ऐसा जिसे battle of plasi कहा गया जिसमें रोबर्ट clive की जीत हुयी 8.    कंपनी का rule 1773 से 1858 तक चला था 9.    ताज का शाशन था 1858 से 1947 10.    Regulating act आया था 1773 में 11.    Act of settlement आया था 1781 में 12.    भारत परिषद् अधिनियम आया था 1861, 1892, 1909 13.    Govt of इंडिया act आया था 1858 में 14.                  ब्रिटिश सरकार ने 1773 में एक regulating act लाया गया जिसमें बंगाल को हेड बनाया गया जिसे गवर्नर जनरल कहा गया बंगा

Linked List Data Structure

Question: How to create without generic Int type Node ? Ans:  public class Node { // this is Node class without Generic int data ; // this is for data like array Element Node next ; //Node ek class hai , usi class ka khud ka variable hai, This is Node(Class) Type variable for //Node is basically refer to class , this is for next element Node ( int data ){ // this is constructor bcse user will pass data value and int because we want to create int type data constructor this . data = data ; // this is refer data next = null ; } }  

Test 2

 Question: You have made a smartphone app and want to set its subscription price such that the profit earned is maximised. There are certain users who will subscribe to your app only if their budget is greater than or equal to your price. You will be provided with a list of size N having budgets of subscribers and you need to return the maximum profit that you can earn. Lets say you decide that price of your app is Rs. x and there are N number of subscribers. So maximum profit you can earn is : m*x Sample input 1: 30 20 53 14 Output 60 import   java . util .*; import   java . util . Scanner ; public   class   solution   {      public   static   int   maximumProfit ( int   budget [])   {      Arrays . sort ( budget );          // int maxProfit = 0;          // // Iterate through each possible subscription price and calculate profit          // for (int i = 0; i < budget.length; i++) {          //     int currentProfit = budget[i] * (budget.length - i);          //     maxProfit = Mat