Java literals
Literals are data used for representing fixed values.
They can be used directly in the code.
For example,
int a = 1;
float b = 2.5;
char c = 'F';
Here, 1, 2.5, and 'F' are literals.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
1. Boolean Literals
In Java, boolean literals are used to initialize boolean data types. They can store two
values: true and false. For example,
boolean flag1 = false;
boolean flag2 = true;
Here, false and true are two boolean literals.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
2. Integer Literals
An integer literal is a numeric value(associated with numbers) without any fractional
or exponential part. There are 4 types of integer literals in Java:
1. binary (base 2)
2. decimal (base 10)
3. octal (base 8)
4. hexadecimal (base 16)
For example:
// binary
int binaryNumber = 0b10010;
// octal In Java, binary starts with 0b, octal starts with
int octalNumber = 027; 0, and hexadecimal starts with 0x.
// decimal
int decNumber = 34;
// hexadecimal
int hexNumber = 0x2F; // 0x represents hexadecimal
// binary
int binNumber = 0b10010; // 0b represents binary
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
3. Floating-point Literals
A floating-point literal is a numeric literal that has either a fractional form or an
exponential form. For example,
class Main {
public static void main(String[] args) {
double myDouble = 3.4;
float myFloat = 3.4F;
// 3.445*10^2
double myDoubleScientific = 3.445e2;
[Link](myDouble); // prints 3.4
[Link](myFloat); // prints 3.4
[Link](myDoubleScientific); // prints 344.5
}
} This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
4. Character Literals
Character literals are unicode character enclosed inside single quotes. For example,
char letter = 'a';
Here, a is the character literal.
We can also use escape sequences as character literals. For example, \b (backspace),
\t (tab), \n (new line), etc.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
5. String literals
A string literal is a sequence of characters enclosed inside double-quotes. For example,
String str1 = "Java Programming";
String str2 = “Talent Battle";
Here, Java Programming and Talent Battle are two string literals.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Java Data Types (Primitive)
There are 8 data types predefined in Java programming language, known as
primitive data types.
Note: In addition to primitive data types, there are also referenced types (object
type).
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
1. boolean type
The boolean data type has two possible values, either true or false.
Default value: false.
They are usually used for true/false conditions.
Example 1: Java boolean data type
class Main {
public static void main(String[] args) {
boolean flag = true;
[Link](flag); // prints true
}
}
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
2. byte type
The byte data type can have values from -128 to 127 (8-bit signed two's complement
integer).
If it's certain that the value of a variable will be within -128 to 127, then it is used
instead of int to save memory.
Default value: 0
Example 2: Java byte data type
class Main {
public static void main(String[] args) {
byte range;
range = 124;
[Link](range); // prints 124
}
}
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
3. short type
The short data type in Java can have values from -32768 to 32767 (16-bit signed two's
complement integer).
If it's certain that the value of a variable will be within -32768 and 32767, then it is
used instead of other integer data types (int, long).
Default value: 0
Example 3: Java short data type
class Main {
public static void main(String[] args) {
short temperature;
temperature = -200;
[Link](temperature); // prints -200
}
}
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
4. int type
The int data type can have values from -2^31 to 2^31-1 (32-bit signed two's
complement integer).
If you are using Java 8 or later, you can use an unsigned 32-bit integer. This will have
a minimum value of 0 and a maximum value of 2^32-1.
Default value: 0
Example 4: Java int data type
class Main {
public static void main(String[] args) {
int range = -4250000;
[Link](range); // print -4250000
}
}
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
5. long type
The long data type can have values from -2^63 to 2^63-1 (64-bit signed two's
complement integer).
If you are using Java 8 or later, you can use an unsigned 64-bit integer with a minimum
value of 0 and a maximum value of 2^64-1.
Default value: 0
Example 5: Java long data type
class LongExample {
public static void main(String[] args) {
long range = -42332200000L;
[Link](range); // prints -42332200000
}
}
Notice, the use of L at the end of -42332200000. This represents that it's an integral
literal of the long type.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
6. double type
The double data type is a double-precision 64-bit floating-point.
It should never be used for precise values such as currency.
Default value: 0.0 (0.0d)
Example 6: Java double data type
class Main {
public static void main(String[] args) {
double number = -42.3;
[Link](number); // prints -42.3
}
}
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
7. float type
The float data type is a single-precision 32-bit floating-point.
It should never be used for precise values such as currency.
Default value: 0.0 (0.0f)
Notice that, we have used -42.3f instead of -42.3in
Example 7: Java float data type the above program. It's because -42.3 is a double
literal.
class Main {
public static void main(String[] args) { To tell the compiler to treat -42.3 as float rather than
double, you need to use f or F.
float number = -42.3f;
[Link](number); // prints -42.3
}
}
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
8. char type
It's a 16-bit Unicode character.
The minimum value of the char data type is '\u0000' (0) and the maximum value of
the is '\uffff'.
Default value: '\u0000’
Example 8: Java char data type
class Main {
public static void main(String[] args) {
char letter = '\u0051';
[Link](letter); // prints Q
}
}
Here, the Unicode value of Q is \u0051. Hence, we get Q as the output.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
String type
Java also provides support for character strings via [Link] class. Strings in
Java are not primitive types. Instead, they are objects. For example,
String myString = "Java Programming";
Here, myString is an object of the String class.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Java Operators
Operators are symbols that perform operations on variables and values.
For example, + is an operator used for addition, while * is also an operator used for
multiplication.
Operators in Java can be classified into below types:
1. Arithmetic Operators
2. Assignment Operators
3. Relational Operators
4. Logical Operators
5. Unary Operators
6. Bitwise Operators
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
1. Java Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on variables and data.
For example,
a + b;
Here, the + operator is used to add two variables a and b. Similarly, there are various
other arithmetic operators in Java.
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Operation (Remainder after division)
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
2. Java Assignment Operators
Assignment operators are used in Java to assign values to variables. For example,
int age;
age = 5;
Here, = is the assignment operator. It assigns the value on its right to the variable on
its left. That is, 5 is assigned to the variable age.
Let's see some more assignment operators available in Java.
Operator Example Equivalent to
= a = b; a = b;
+= a += b; a = a + b;
-= a -= b; a = a - b;
*= a *= b; a = a * b;
/= a /= b; a = a / b;
%= a %= b; a = a % b;
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
3. Java Relational Operators
Relational operators are used to check the relationship between two operands. For
example,
// check is a is less than b
a < b;
Here, > operator is the relational operator. It checks if a is less than b or not.
It returns either true or false.
Operator Description Example
== Is Equal To 3 == 5 returns false
!= Not Equal To 3 != 5 returns true
> Greater Than 3 > 5 returns false
< Less Than 3 < 5 returns true
>= Greater Than or Equal To 3 >= 5 returns false
<= Less Than or Equal To 3 <= 5 returns true
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
4. Java Logical Operators
Logical operators are used to check whether an expression is true or false. They are
used in decision making.
Operator Example Meaning
true only if both
expression1 &&
&& (Logical AND) expression1 and
expression2
expression2 are true
true if either
expression1 ||
|| (Logical OR) expression1 or
expression2
expression2 is true
true if expression is
! (Logical NOT) !expression
false and vice versa
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
5. Java Unary Operators
Unary operators are used with only one operand. For example, ++ is a unary operator
that increases the value of a variable by 1. That is, ++5 will return 6.
Different types of unary operators are:
Operator Meaning
+ Unary plus: not necessary to use since numbers are positive without using it
- Unary minus: inverts the sign of an expression
++ Increment operator: increments value by 1
-- Decrement operator: decrements value by 1
! Logical complement operator: inverts the value of a boolean
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
6. Java Bitwise Operators
Bitwise operators in Java are used to perform operations on individual bits. For example,
Bitwise complement Operation of 35
35 = 00100011 (In Binary)
~ 00100011
________
11011100 = 220 (In decimal)
Here, ~ is a bitwise operator. It inverts the value of each bit (0 to 1 and 1 to 0).
Operator Description
~ Bitwise Complement
<< Left Shift
>> Right Shift
>>> Unsigned Right Shift
& Bitwise AND
^ Bitwise exclusive OR
These operators are not generally used in Java.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Java instanceof Operator
The instanceof operator checks whether an object is an instanceof a particular class.
For example,
class Main {
public static void main(String[] args) {
String str = "Programming";
boolean result;
// checks if str is an instance of
// the String class
result = str instanceof String;
[Link]("Is str an object of String? " + result);
}
}
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Java Ternary Operator
The ternary operator (conditional operator) is shorthand for the if-then-else statement.
For example,
variable = Expression ? expression1 : expression2
If the Expression is true, expression1 is assigned to the variable.
If the Expression is false, expression2 is assigned to the variable.
class Java {
public static void main(String[] args) {
int februaryDays = 29;
String result;
// ternary operator
result = (februaryDays == 28) ? "Not a leap year" : "Leap year";
[Link](result);
}
} This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Java Basic Input and Output
Java Output
In Java, you can simply use
[Link](); or
[Link](); or
[Link]();
to send output to standard output (screen).
Here,
System is a class
out is a public static field: it accepts output data.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Difference between println(), print() and printf()
print() - It prints string inside the quotes.
println() - It prints string inside the quotes similar like print() method. Then the
cursor moves to the beginning of the next line.
printf() - It provides string formatting (similar to printf in C/C++ programming).
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Example: Printing Variables and Literals
class Variables {
public static void main(String[] args) {
Double number = -10.6;
[Link](5);
[Link](number);
}
}
When you run the program, the output will be:
5
-10.6
Here, you can see that we have not used the quotation marks. It is because to
display integers, variables and so on, we don't use quotation marks.
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video
Example: Print Concatenated Strings
class PrintVariables {
public static void main(String[] args) {
Double number = -10.6;
[Link]("I am " + "awesome.");
[Link]("Number = " + number);
}
}
Output:
I am awesome.
Number = -10.6
In the above example, notice the line,
[Link]("I am " + "awesome.");
This video is sole property of Talent Battle Pvt. Ltd. Strict penal action will be taken against unauthorized piracy of this video