What is Java String ?
A String may be defined as a sequence of characters , A String can be instantiated as follows:
1. String Name = "Sachin Malhotra";
2. String Name = new String("Sachin");
"String is a class in java but it can be used as a primitive/non primitive data type"
STRING NAME = "SACHIN MALHOTRA" , where NAME is reference variable and "Sachin malhotra" is a string literal.
You can print string in different ways
[a] System.out.println(); // with new line
[b] System.out.println(); // with no new line
[c] System.out.printf(); // with no new line
[d] System.out.format(); // with no new line
"NOTE : String are immutable and cannot be changed it can be changed by only performing some operation on other string and copy that into it"
What is a format specifier ?
A Format Specifier shows us the type of format for decimal , float , character , and string such as
%d for integer
%f for float
%c for character
%s for string
Please Download The PDF notes by clicking here : Click Here
CODE SNIPPET FOR ABOVE NOTES IS MENTIONED BELOW
package stringsjava.pkg1;
public class StringsJava1 {
public static void main(String[] args) {
/*
String in java
String is basically a sequence of characters
Strings cannot be changed because they are immutable
they can be copied from one string to other for operations
"C L A S S G E N I X"
0 1 2 3 4 5 6 7 8 9
*/
// first way of creating string
String Name = "Welcome to my channel"; // Syntax of a string
// String = Java Class
// Name = Variable Name of a string
// "Welcome to my channel" = String literal
// second way of creating string
String name = new String("Hello World"); // syntax of the string
// String = Java Class
// Name = Variable Name of a string
// "Welcome to my channel" = String literal
// new = new keyword which helps in making new object of a string
// four ways to print a string in java
// first
System.out.println(Name); // it is changing to new line
// second
System.out.print(Name); // it is not changing to new line
// third
System.out.printf(Name); // it is not changing to new line
// fourth
System.out.format(Name); // it is not changing to new line
// format specifier in java stringm= '%d' , '%f' , '%s' and '%c'
/*
%d for decimal
%f for float
%c for character
%s for string
*/
String NAME = "Prabhjot Kaur";
int marks = 98;
String Subject = "English";
float percentage = 98;
System.out.printf("\nThe girl name is %s who scored %d in %s ",NAME,marks,Subject);
System.out.printf("She got %.1f in B.Tech 2021 ",percentage);
}
}