Java - Class , Objects, Variables , Data Types, Packages - best tricks forever

BEGINNER GUIDE (Hindi)

test banner

Post Top Ad

Responsive Ads Here

Thursday, 12 April 2018

Java - Class , Objects, Variables , Data Types, Packages


Java - Class , Objects, Variables , Data Types, Packages

Class :


what is a Java Class ?

Class is defined as the template which is used to create objects or methods which is used for common purpose.

Since Java is purely and Object-Oriented Programming language, Java template class is used to create many number of objects. 

how class can be declared in java program ?

Class is declared as java-keyword class followed by ClassName with open and closed braces.

class ClassName{

public static void main(String args[])
{
}
}

An access specifier may or may not be applied in front of the java-keyword class.

so what is an access specifier ?

Access specifier is a java-keyword which determines the accessiblity of the class,methods and objects.

there are 4 types of access specifiers

1. public
2. private
3. protected
4. default

1. public

It is a java-keyword which allows the accessibility of class, objects, methods, variables and other members from anywhere, since its mentioned as public class.

public class ClassName{
public static void main(String args[])
{

}
}

Above class is accessible from anywhere inside the package

2. private 


It is a java-keyword which allows the accessibility of class, objects, methods, variables and other members only inside the public class.

Since it is a private class other public classes cannot access this private class.

But private classes can be accessible by when a public class have inner class as private class

public class ClassName{
public static void main(String args[])
{

}
private class ClassNameOne {

}
}

3. Protected

It is a java keyword which allows the accessibility class, objects, methods, variables and other members from any class inside the same package.

Classes cannot be declared protected. This access modifier is generally used in a parent child relationship.

class 1 :

package abcpackage;
public class ClassOne {

protected int add(int a, int b){
return a+b;
}
}

class 2 :

package xyzpackage;
import abcpackage.*;

class ClassTwo extends ClassOne{

public static void main(String args[]){
ClassTwo obj = new ClassTwo();
System.out.println(obj.add(11, 22));
}
}

4. Default


When no access specifier is mentioned by default java considers as public (access specifier)

Package :


So what is a java package ?

Java package is the namespace which is declared to organise the set of related classes and its interfaces.

We have to define the package name on the top of the .java file.


There are two types of packages

1) User defined package:


        The package created by us is called user-defined package, which is also called as custom package.

package packageName;

public class ClassName {
public static void main(String args[])
{

}
}

2) Built-in package:

        Java already have some pre defined package also called as built-in package like java.io.*, java.lang.* etc

if we want to use the java build-in package, then we have to import the package name.

package packageName;
import java.io.*;
import java.lang.*;

public class ClassName{
public static void main(String args[])
{

}
}


So Class is the main thing in Java, and there are common types of class listed below.

1. Wrapper Class

2. Mutable Class

3. Abstract Class

4. Final Class

5. Anonymous Class

6. Input-Output Class

7. String Class

8. System Class

9. Network Class

Note : Above classes will be explained in detail later on in this tutorial series.


Objects :

Java class is the template or buleprint used for creating objects.

Objects are also called as instance of the class which can be combination of variables, methods and other members.

Objects can be created by using the java-keyword "new".

public class ClassName 
{
String name = "Sample Name";
public static void main(String[] args)
{
ClassName obj = new ClassName();
System.out.println(obj.name);
}
}

Output :

Sample Name


We can also create multiple objects inside the class

public class ClassName
{
String name = "Sample Name";
int age = 10;
String sex = "Male"
String location = "India";
public static void main(String[] args)
{
ClassName obj1 = new ClassName();
ClassName obj2 = new ClassName();
ClassName obj3 = new ClassName();
ClassName obj4 = new ClassName();
System.out.println(obj1.name);
System.out.println(obj2.age);
System.out.println(obj3.sex);
System.out.println(obj4.location);
}
}

Output :

Sample Name
10
Male
India

Variable :

what is a variable ?

Variable is the one which is liable to change, it prefers changing from one value to another.

Variables in programming language have same qualities as mentioned above.

so what is a variable in programming language ?

Variable is simply a value which have the ability to change itself depending on the conditions passed to the program.

what is variable in Java ?

In Java variable is the Name given to the allocated memory location inside the memory, and variables should be declared with predefined attribute called data-types.



Date types :

what is data type ?

data type is an attribute that a data value can have.

its defines the type of value that a data can hold in a programming language.

it also tells the characteristics (predefined attribute) of the data value to the compiler for the variables.

There are 8 types of datatypes

1. byte

Byte is the defined as the smallest integer data-type.
Byte variables are declared by use of the java-keyword byte.

byte a = 100;

Range : It has a minimum value of -128 and a maximum value of 127 (inclusive)

2. short

Short variables are declared by using the java-keyword short.
The short data type is a 16-bit type.

short a = 2300;

Range : It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive)

3. int

Int variables are declared by using the java-keyword int.
It is a signed 32-bit type.

int a = 12345;

Range : has a range from –2,147,483,648 to 2,147,483,647 (inclusive)

4. long


Long variables are declared by using the java-keyword long.
It is a signed 64-bit type.
It is used for very large number variables.

long a = 123456789;

Range : It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive).

5. float


Float variables are declared by using the java-keyword float.
The type float specifies a single-precision value that uses 32 bits of storage.
Float type is a decimal value type and syntax is denoted by adding "f" after the initialing the variable.

float a = 12.34f;

Range : It have the range from 1.4e-045 to 3.4e+038 approx.

6. double

Double variables are declared by using the java-keyword double.
The double keyword, uses 64 bits to store a value.
Its a higher level decimal value type and syntax is denoted by adding "d" after the initialing the variable.

double a = 12345.678d;

Range : It have the range from Approx range 4.9e-324 to 1.8e+308 appox.

7. boolean


Boolean variables are declared by using the java-keyword boolean.
Boolean data type has only two possible values: true and false.
It is a signed 1-bit type.

boolean a = true;
boolean b = false;

8. char


Char variables are declared by using the java-keyword char.
The data type used to store characters is char.
It is a single 16-bit Unicode character.

char a = 'A';

Range : It have minimum value Unicode 0 and maximum value Unicode 216-1





Byte, Short , Int , Long



S.no
Data Types
size(bites)
Range
1
Byte
1
+127 to -128
2
Short
2
+32767 to -32768
3
Int
4
+2147483647 to -2147483648
4
Long
8
±9.223*1018


Float and Double



S.no
Data Types
size(bites)
Range
Number of Decimal places
1
Float
4
+2147483647 to -2147483648
8
2
Double
8
±9.223*1010
16



Char



S.no
Data Types
size(bites)
Range
Number of Decimal places
1
Char
2
+32767 to -32768
8























Note :  Hi i am new for blogging, if you think my blog is useful, kindly SUBSCRIBE  in the website and also share this to your friends. And if anything is wrong in the tutorial please correct me and kindly forgive me. Thanks everyone.





No comments:

Post a Comment

Post Bottom Ad

Responsive Ads Here