Professional Documents
Culture Documents
Char Type1
Char Type1
Note: The default value of the char variable is a null character, not a single space.
Compared to C lang, in Java lang char data type size is 2 bytes because the Java char
data type is used to represent the Unicode character set. Unicode character set range
is 0-65535, which occupies 2 bytes of memory.
C lang char data type size is 1 byte because C lang supports only the ASCII character set
which contains 0-255 characters. Hence 1 byte memory is sufficient.
In a Java program to store true and false, we must use a boolean variable.
The boolean values are meant for performing validations and taking decisions on
whether to execute a block of statements or not.
In regular programming, we don’t store boolean values in a variable like other data
values. We will use boolean values with if, while and for loops.
Ex:
In C language, we use 0 as false and 1 as true. But in Java, we can’t use this
way.
In The above example, the bo value is read from the memory, compared with the right-side
value(true), and the result is returned to the if loop.
String Data Type
The String is a referenced data type. It is a class. It is a predefined class created by
SUN. It is available in java.lang package in java.base module.
It is used for representing a sequence of characters. For storing a sequence of
characters as one group we must create a String class type reference variable as shown
below.
Rule: If we want the compiler and JVM to treat a sequence of characters as String type,
we must place them inside double quotes(""). If we use a sequence of characters
directly in a program, the compiler will not treat it as a String, it will throw an error.
The text without double quotes will be treated as a variable.
We can use all predefined and user-defined class names as variable names.
Note: We are allowed to create our own class with the name String. If we create our
own class with the name String, we must access the pre-defined String class with its
fully qualified name. It means we must access it by using its package name.
If we try to compile the above Example class, the parameter String[] is linked with the class
String(We created the class String).
String data type
o size is number characters *2 --> 2 is because each character can take 2 bytes.
o range no limit, you can store any number of characters.
Ex: String name = “Murali”; takes 6 * 2 --> 12 bytes of memory.
o default value is null.
Remember String is not always a predefined class. The name String will be considered a
predefined class as long as we do not create our own class with the name String. If we
create our own class with the name String, then the class name String will be referred
to our local class String but not a predefined class. If we refer to a predefined class
String, we must explicitly use the class name as java.lang.String.
Hence, we can say the statement String s1 = "Hari"; is not compiled and
public static void main(String[] args] will not be executed always Then,
java.lang.String s1 = "Hari"; and public static void
main(java.lang.String[] args) are always compiled and executed.