Java Mynotes
Java Mynotes
Java Mynotes
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.
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.
A local variable cannot be defined with "static" keyword.
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.
3) Static variable
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.
Java Primitive Data Types
In Java language, primitive data types are the building blocks of data manipulation.
These are the most basic data types available in Java language.
boolean data type: The Boolean data type is used to store only two possible
values: true and false. This data type is used for simple flags that track
true/false conditions. The Boolean data type specifies one bit of information,
but its "size" can't be defined precisely.
byte data type : The byte data type is an example of primitive data type. It
isan 8-bit signed two's complement integer. Its value-range lies between -
128 to 127 (inclusive). Its minimum value is -128 and maximum value is
127. Its default value is 0.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.
char data type : The char data type is a single 16-bit Unicode character. Its
value-range lies between '\u0000' (or 0) to '\uffff' (or 65,535 inclusive).The
char data type is used to store characters.
short data type : The short data type is a 16-bit signed two's complement
integer. Its value-range lies between -32,768 to 32,767 (inclusive). Its
minimum value is -32,768 and maximum value is 32,767. Its default value is
0.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.
Int data type : The int data type is a 32-bit signed two's complement
integer. Its value-range lies between - 2,147,483,648 (-2^31) to
2,147,483,647 (2^31 -1) (inclusive). Its minimum value is -
2,147,483,648and maximum value is 2,147,483,647. Its default value is
0.The int data type is generally used as a default data type for integral values
unless if there is no problem about memory.
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.
float data type : The float data type is a single-precision 32-bit IEEE 754
floating point. Its value range is unlimited. It is recommended to use a float
(instead of double) if you need to save memory in large arrays of floating
point numbers. The float data type should never be used for precise values,
such as currency. Its default value is 0.0F.
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.
Q) Why char uses 2 byte in java and what is \u0000 ?
It is because java uses Unicode system not ASCII code system. The \u0000 is the
lowest range of Unicode system. To get detail explanation about Unicode visit next
page.
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:
The Java unary operators require only one operand. Unary operators are used to
perform various operations i.e.:
The Java left shift operator << is used to shift all of the bits in a value to the left
side of a specified number of times.
The Java right shift operator >> is used to move the value of the left operand to
right by the number of bits specified by the right operand.
Java AND Operator Example: Logical && and Bitwise &
The logical && operator doesn't check the second condition if the first condition is
false. It checks the second condition only if the first one is true.
The bitwise & operator always checks both conditions whether first condition is
true or false.
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.
Java assignment operator is one of the most common operators. It is used to assign
the value on its right to the operand on its left.
Java Keywords
Java keywords are also known as reserved words. Keywords are particular words
that act as a key to a code. These are predefined words by Java so they cannot be
used as a variable or object name or class name.
Class Definition
In object-oriented programming, a class is a basic building block. It can be defined
as template that describes the data and behavior associated with the class
instantiation. Instantiating is a class is to create an object (variable) of that class
that can be used to access the member variables and methods of the class.
A class can also be called a logical template to create the objects that share
common properties and methods.
For example, an Employee class may contain all the employee details in the form
of variables and methods. If the class is instantiated i.e. if an object of the class is
created (say e1), we can access all the methods or properties of the class.
EXAMPLE:
int x = 5;
Create an Object
In Java, an object is created from a class. o create an object of Main, specify the
class name, followed by the object name, and use the keyword new:
int x = 5;
System.out.println(myObj.x);
}
Constructors in Java
In Java, a constructor is a block of codes similar to the method. It is called when an
instance of the class is created. At the time of calling constructor, memory for the
object is allocated in the memory.
Every time an object is created using the new() keyword, at least one
constructor is called.
class Bike1{
Bike1(){System.out.println("Bike is created");}
In this example, we have created the constructor of Student class that have two
parameters. We can have any number of parameters in the constructor.
class Student4{
int id;
String name;
id = i;
name = n;
s1.display();
s2.display();
Output:
111 Karan
222 Aryan
In Java, a constructor is just like a method but without return type. It can also be
overloaded like Java methods.
int id;
String name;
int age;
id = i;
name = n;
id = i;
name = n;
age=a;
}
s1.display();
s2.display();
222 Aryan 25
Q) Does constructor return any value?
Yes, it is the current class instance (You cannot use return type yet it returns a
value).
Yes, like object creation, starting a thread, calling a method, etc. You can perform
any operation in the constructor as you perform in the method.
Yes.
Q) What is the purpose of Constructor class?
Java provides a Constructor class which can be used to get the internal information
of a constructor in the class. It is found in the java.lang.reflect package.