You are on page 1of 10

char Data Type

About character value:


If we try to use a letter without placing it in a single quotation, then it will be
considered a variable.

 If we place a character in a single quotation, then it will be considered a character.


System.out.println(1); // the value 1 is int type
System.out.println(‘1’); // the value 1 is char type.
 Any single alphabet, digit or special character placed inside a single quote is called a
character.
Ex:

 Every character by default is treated as a char type.

 To store a single character, we must create a char variable.


Ex:

 Inside a single quote


o only one character is allowed.
o only one space is allowed.
o empty character is not allowed.
 To represent the data with multiple characters, we need to use the String.

Size, range and default value of char data type:

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.

Java supports Unicode character set for developing InternationalizatioN(118N)


application 18N application means it should display content in the current country's
native language in which it is running.
boolean Data Type
 Java supports a special data type called boolean for representing conditional/logical
values true and false.
 By default, true and false are considered as boolean types.

 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 projects, we do not directly place boolean values true/false in if()/while()/for()


condition as shown in the above code rather we will place an expression that generates
a boolean value true/false dynamically with the given input values.
 To generate boolean values dynamically, we must prepare an expression using the
below 3 operators:
1. Relational Operators(<, >, <=, >=, instanceof)
2. Equality Operators(==, !=)
3. Logical Operators(&&, ||, !)

 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.

 When we want to store a name, course, department name, or an alpha-numeric


number like PAN Number, Engine Number, bike Number etc., we must use a String data
type variable.

 Inside double quotes( ""), we can 0 or n number of characters. It means an empty


String is allowed.
 Text placed inside the double quotes is considered as String type by the compiler and
JVM.

 We can use all predefined and user-defined class names as variable names.

Q ) What is the output of the below code snippet?


String String = “String”;
S.o.p(String);

 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.

You might also like