Java T Point Notes-converted (4)
Java T Point Notes-converted (4)
1. Standalone application
2. Web based application
3. Enterprise application
4. Mobile application
Variables:
Variables are nothing but piece of memory use to store information. One variable can
store single information at a time. To utilize variables in java programming language we need to
follow below steps:
1. Variable Declaration (Allocating / Reserving memory)
2. Variable Initialization (Assigning / Inserting value)
3. Variable Usage
Note: - According to all programming language dealing with information directly is not a good
practice. To overcome this problem variables are introduced. There are four types of the variable.
1. Local Variable: A variable declared inside the body of method or constructor is called as
Local Variable. It is also called as temporary variable. The scope of local variable is
within the block in which it is declared. The local variable cannot be ‘Static’ in nature.
2. Global Variable: A variable which is declared in the class but outside the method body is
called as Global variable. It is also called as Permanent Variable. Scope of global variable
is present within the class only. It cannot be ‘Static’ in nature. Global variables without
static word cannot be used in static methods. It is also called as Instance variable.
They are created when we create object by using new keyword and destroyed as we
destroy object. Instance variables have default values.
3. Static Variable: The variable used with Static keyword and declared in the class
body is called as Static variable. It is also called as Class variables. It is stored in
static memory.
Data Types: Data types are used to represent which type of information we are going to use in
our program. Depending on the data type memory reservation for the variable is decided. As per
the syntax of variable declaration we have to declare its data type first.
Syntax : data_type variable_name = value;
Example: int a = 10;
There are two main types of data types: Primitive data type and Non-primitive data types.
1. Primitive data types: Those data types have fix memory size are called as Primitive data
type. There are total 8 primitive data types. Primitive data types are again divide into two
types.
Data types Size Range Default value Example
Byte 1 byte -128 to 127 0 byte b = 122;
Short 2 bytes -32768 to 32767 0 short s = 30000;
Int 4 bytes -2^31 to [(2^31)-1] 0 int I = 51000;
Long 8 bytes -2^63 to [(2^63)-1] 0 long m = 2147483650
Float 4 byte -3.4e38 to 3.4e38 0.0 float f = 34.1234;
Double 8 byte -1.7e308 to 1.7e308 0.0 double d= 914748.365
Char 2 bytes 0 to 65535 0 (Space character) char c = ‘a’;
Boolean N.A. Only true or false False boolean a= true;
Objects: Entities which have state, behavior and identity is called as Objects. An object is
instance (result) of class. For example, chair, pen, table, dog. Dog has his own name, color
that comes under the state and barking, running comes under behavior. Entity may be
logical and physical.
Class: Class is a blueprint from which we can create objects. It is logical entity. I cannot be
physical entity. It is group of common objects. Class in java consists of blocks, methods,
variables, interface etc.
Methods: A method in java is a block of code or collection of statements used to perform a
specific operation. It is used to achieve the reusability of code. We can write method once
and use it again and again. It is a block of code which runs when we call it.
Method signature () is used to pass the parameter to the method.
Syntax for method declaration: access modifier return type method name()
Example: public void add() { method body }
Methods are mainly divided into two types. Pre-defined methods and user defined methods
1. Predefined Methods: Methods which are already defines in java class libraries are
called as Pre-defined methods. These methods cannot be modified. Main method is one
of the best examples of predefined method. JVM always start executing code from main
method. Length(), equals(), sqrt() these are the examples of pre-defined methods.
2. User Defined methods: Methods which are written by the user or programmer is called
as User defined methods. These methods can be modified as per the requirement.
3. Static Method: The method which is created by using static keyword and belongs to
class is called as Static Method. We can call static method without creating object.
Syntax: class_name.method_name(); example: sample1.m1();
4. Non-Static method: It is also called as Instance method. To call these methods we have
to create an object of the class.
Syntax: class_name variable_name = new class_name();
Variable_name.method_name();
1. Static method calling from same class