Java Unit1
Java Unit1
Java Unit1
The Java comments are the statements in a program that are not executed by the
compiler and interpreter.
Single line comments starts with two forward slashes (//). Any text in front of // is
not executed by Java.
Syntax:
1. //This is single line comment
CommentExample1.java
1. public class CommentExample1 {
2. public static void main(String[] args) {
3. int i=10; // i is a variable with value 10
4. System.out.println(i); //printing the variable i
5. }
6. }
Output:
10
Multi-line comments are placed between /* and */. Any text between /* and */ is not
executed by Java.
Syntax:
1. /*
2. This
3. is
4. multi line
5. comment
6. */
CommentExample2.java
1. public class CommentExample2 {
2. public static void main(String[] args) {
3. /* Let's declare and
4. print variable in java. */
5. int i=10;
6. System.out.println(i);
7. /* float j = 5.9;
8. float k = 4.4;
9. System.out.println( j + k ); */
10. }
11. }
Output:
10
Syntax:
1. /**
2. *
3. *We can use various tags to depict the parameter
4. *or heading or author name
5. *We can also use HTML tags
6. *
7. */
Data Types in Java
Data types specify the different sizes and values that can be stored in the variable.
There are two types of data types in Java:
1. Primitive data types: The primitive data types include boolean, char, byte, short, int,
long, float and double.
2. Non-primitive data types: The non-primitive data types include Classes, Interfaces,
and Arrays.
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
Example:
1. Boolean one = false
The byte data type is used to save memory in large arrays where the memory savings
is most required. It saves space because a byte is 4 times smaller than an integer. It
can also be used in place of "int" data type.
Example:
1. byte a = 10, byte b = -20
The short data type can also be used to save memory just like byte data type. A short
data type is 2 times smaller than an integer.
Example:
1. short s = 10000, short r = -5000
The int data type is generally used as a default data type for integral values unless if
there is no problem about memory.
Example:
1. int a = 100000, int b = -200000
Long Data Type
The long data type is a 64-bit two's complement integer. Its value-range lies between
-9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1)(inclusive).
Its minimum value is - 9,223,372,036,854,775,808and maximum value is
9,223,372,036,854,775,807. Its default value is 0. The long data type is used when you
need a range of values more than those provided by int.
Example:
1. long a = 100000L, long b = -200000L
Example:
1. float f1 = 234.5f
Double Data Type
The double data type is a double-precision 64-bit IEEE 754 floating point. Its value
range is unlimited. The double data type is generally used for decimal values just like
float. The double data type also should never be used for precise values, such as
currency. Its default value is 0.0d.
Example:
1. double d1 = 12.3
Example:
1. char letterA = 'A'
Java Variables
A variable is a container which holds the value while the Java program is executed. A
variable is assigned with a data type.
Variable is a name of memory location. There are three types of variables in java: local,
instance and static.
There are two types of data types in Java: primitive and non-primitive.
Variable
A variable is the name of a reserved area allocated in memory. In other words, it is a
name of the memory location. It is a combination of "vary + able" which means its
value can be changed.
1. int data=50;//Here data is variable
Types of Variables
There are three types of variables in Java:
o local variable
o instance variable
o static variable
1) Local Variable
A variable declared inside the body of the method is called local variable. You can
use this variable only within that method and the other methods in the class aren't
even aware that the variable exists.
2) Instance Variable
A variable declared inside the class but outside the body of the method, is called an
instance variable. It is not declared as static.
It is called an instance variable because its value is instance-specific and is not shared
among instances.
3) Static variable
A variable that is declared as static is called a static variable. It cannot be local. You
can create a single copy of the static variable and share it among all the instances of
the class. Memory allocation for static variables happens only once when the class is
loaded in the memory.
Output:
20
Output:
10
10.0
Output:
10.5
10
Output:
130
-126
Output:
20
Operators in Java
Operator in Java is a symbol that is used to perform operations. For example: +, -,
*, / etc.
There are many types of operators in Java which are given below:
o Unary Operator,
o Arithmetic Operator,
o Shift Operator,
o Relational Operator,
o Bitwise Operator,
o Logical Operator,
o Ternary Operator and
o Assignment Operator.
prefix ++expr --expr +expr -expr ~ !
Arithmetic multiplicative * / %
additive + -
Shift shift << >> >>>
equality == !=
bitwise exclusive OR ^
bitwise inclusive OR |
logical OR ||
Ternary ternary ? :
Output:
10
12
12
10
Output:
22
21
Output:
-11
9
false
true
Java Arithmetic Operators
Java arithmetic operators are used to perform addition, subtraction, multiplication,
and division. They act as basic mathematical operations.
Output:
15
5
50
2
0
Output:
21
Output:
40
80
80
240
Output:
2
5
2
Output:
5
5
-5
1073741819
The bitwise & operator always checks both conditions whether first condition is true
or false.
1. public class OperatorExample{
2. public static void main(String args[]){
3. int a=10;
4. int b=5;
5. int c=20;
6. System.out.println(a<b&&a<c);//false && true = false
7. System.out.println(a<b&a<c);//false & true = false
8. }}
Output:
false
false
Output:
false
10
false
11
Java OR Operator Example: Logical || and Bitwise |
The logical || operator doesn't check the second condition if the first condition is
true. It checks the second condition only if the first one is false.
The bitwise | operator always checks both conditions whether first condition is true
or false.
1. public class OperatorExample{
2. public static void main(String args[]){
3. int a=10;
4. int b=5;
5. int c=20;
6. System.out.println(a>b||a<c);//true || true = true
7. System.out.println(a>b|a<c);//true | true = true
8. //|| vs |
9. System.out.println(a>b||a++<c);//true || true = true
10. System.out.println(a);//10 because second condition is not checked
11. System.out.println(a>b|a++<c);//true | true = true
12. System.out.println(a);//11 because second condition is checked
13. }}
Output:
true
true
true
10
true
11
Output:
Another Example:
1. public class OperatorExample{
2. public static void main(String args[]){
3. int a=10;
4. int b=5;
5. int min=(a<b)?a:b;
6. System.out.println(min);
7. }}
Output:
Output:
14
16
Output:
13
9
18
9
Output:
1. public class OperatorExample{
2. public static void main(String args[]){
3. short a=10;
4. short b=10;
5. a=(short)(a+b);//20 which is int now converted to short
6. System.out.println(a);
7. }}
Output:
20
Syntax:
1. Destination_datatype = (target_datatype) variable;
2. (data_type) it is known as casting operator
Suppose, we want to convert the float data type into int data type. Here, the target
data type is smaller than the source data because the size of int is 2 bytes, and the
size of the float data type is 4 bytes. And when we change it, the value of the float
variable is truncated and convert into an integer variable. Casting can be done with a
compatible and non-compatible data type.
1. float b = 3.0;
2. int a = (int) b; // converting a float value into integer
AreaOfRectangle.c
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
5. printf("\n Welcome to Javatpoint tutorials ");
6. float x = 3.5, y = 4.5; // the size of float variable is 4 byte.
7. int area; // the size of the int variable is 2 bytes.
8. area = (int) x * y; // after conversion the product converts into integer
9. printf("\n Area of a Rectangle is : %d", area);
10. printf("\n Here, we convert float data type into the Int data type");
11. getch();
12. }
Output:
Suppose, we have an int data type and want to convert it into a float data type.
These are data types compatible with each other because their types are numeric,
and the size of int is 2 bytes which is smaller than float data type. Hence, the
compiler automatically converts the data types without losing or truncating the
values.
1. int a = 20;
2. Float b;
3. b = a; // Now the value of variable b is 20.000 /* It defines the conversion of int data type to
float data type without losing the information. */
In the above example, the int data type is converted into the float, which has a larger
size than int, and hence it widens the source data type.
1. #include<stdio.h>
2. #include<conio.h>
3. void main()
4. {
5. printf("\n Welcome to Javatpoint tutorials ");
6. int x = 3, y = 4; // the size of int variable is 2 byte.
7. float area; // the size of float variable is 4 bytes.
8. area = x * y; /* It is a type conversion that automatically converted by the compiler at the
compile time of a program. */
9. printf("\n Area of a Rectangle is : %f", area);
10. printf("\n Here, we convert int data type to the float data type");
11. getch();
12. }
Output:
2 It can be used both compatible data type Type conversion is only used with compatible
and incompatible data type. data types, and hence it does not require any
casting operator.
4 It is used while designing a program by It is used or take place at the compile time of a
the programmer. program.
5 When casting one data type to another, When converting one data type to another, the
the destination data type must be destination type should be greater than the
smaller than the source data. source data type.