- Beranda
- Programmer Forum
Java String
...
TS
itian.kongu
Java String
What is a string?
In C, C++, a string represents an array of characters (last character ends with ‘\0’).
In Java, a string represents the object of String class. We also have character array in java.
Is String a class or a datatype?
String is a class defined in java.lang package. In Java, every class is considered as user defined data type. So we can take string as a data type.
Ways to create a String:
Method 1: By assigning a group of characters to a string type variable.
Syntax:
String <variable_name>;
<varaible_name> = “string”;
Example:
String name;
name = “Hari”;
Declaration and assignment can be made by a single statement. This is just a simplification of the above.
Syntax:
String <variable_name> = value;
Example:
String name = “Hari”;
Method 2: Creating an object to string class by allocating memory using new operator.
Syntax:
String <object> = new String(“value”);
Example:
String name = new String(“Hari”);
Method 3: Converting the character array into strings.
Syntax:
String <object> = new String(<Char_Array>
Example:
char n[] = {‘H’,’a’,’r’,’i’};
String name = new String(n);
Note: A string can be empty with no characters inside it.
Example:
String s=””
To Know more about Java Strings Visit
String
String Buffer
String Builder
In C, C++, a string represents an array of characters (last character ends with ‘\0’).
In Java, a string represents the object of String class. We also have character array in java.
Is String a class or a datatype?
String is a class defined in java.lang package. In Java, every class is considered as user defined data type. So we can take string as a data type.
Ways to create a String:
Method 1: By assigning a group of characters to a string type variable.
Syntax:
String <variable_name>;
<varaible_name> = “string”;
Example:
String name;
name = “Hari”;
Declaration and assignment can be made by a single statement. This is just a simplification of the above.
Syntax:
String <variable_name> = value;
Example:
String name = “Hari”;
Method 2: Creating an object to string class by allocating memory using new operator.
Syntax:
String <object> = new String(“value”);
Example:
String name = new String(“Hari”);
Method 3: Converting the character array into strings.
Syntax:
String <object> = new String(<Char_Array>
Example:
char n[] = {‘H’,’a’,’r’,’i’};
String name = new String(n);
Note: A string can be empty with no characters inside it.
Example:
String s=””
To Know more about Java Strings Visit
String
String Buffer
String Builder
0
710
4
Komentar yang asik ya
Mari bergabung, dapatkan informasi dan teman baru!
Programmer Forum
20.2KThread•4.5KAnggota
Urutkan
Terlama
Komentar yang asik ya