Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Saturday, 9 June 2018

Chaudhari Karan

Java – Modifier Types

By     No comments:

Java – Modifier Types

Hello friends, In this lecture, we will discuss Java Modifier types.
Java – Modifier Types

There are two types of Modifier.
  • Java Access Modifiers
  • Non Access Modifiers

Java Access Modifiers

Java has a number of access modifiers. That modifier set access levels for classes, variables, methods, and constructors. Here are four access levels:
  • It is visible to the package, the default. No modifiers are needed.
  • Visible to the private class only.
  • Visible to the public world.
  • Visible to the package and all protected subclasses.

Default Access Modifier - No Keyword

It means do not explicitly declare an access modifier for the class, field, method, etc.

A variable and method declared without access modifier. It is available to other class in the same package. The fields in the interface are implicitly public static final and the methods in the interface are by default public.

Variable and method declared without any access modifier.
Default Access Modifier - No Keyword

Private Access Modifier - Private

Methods, variables, and constructors are declared as private. It can access the declared class itself.

Private access modifier is the restrictive access level modifier.

Class and interfaces are not private.

Variables are declared as private. It can access outside the class if public methods are present in the class.

The private modifier is the object encapsulates itself and hides data from the outside world.

Java Private Access Modifier - Private

Here, the format variable is private in Example class, but there is no way for other classes to retrieve or set its value directly.

This variable available is the outside world, here is defined two public methods: getFormat(), which returns the value of format, and setFormat(String format), which sets its value.

Public Access Modifier – Public

Any class, method, constructor, interface, etc. declared as public. It can be accessed from other classes. But fields, methods, and blocks declared inside the public class which accessed from any class.

If the public class is trying to access in the different package.

Because of class inheritance method, all public methods and variables of the class are inherited by subclasses.


Java Public Access Modifier – Public

The main() method of an application is public. Otherwise, it not called by a Java interpreter to run the class.

Protected Access Modifier - Protected

Variables, methods, and constructors,  declared as protected in the superclass. It accessed by the subclasses in other package or class with the package of the protected members' class.

The protected access modifier is not applied to the class and interfaces. Methods, fields are declared as protected, but methods and fields in the interface are not declared protected.

Protected access the subclass to use the method or variable while preventing the nonrelated class.

Java Protected Access Modifier - Protected

Access Control and Inheritance

Here is rules for inherited methods are enforced:
  • Methods are declared public in the superclass and must be public in all subclasses. 
  • Methods are declared protected in the superclass and must be protected or public in subclasses; it cannot be private. 
  • Methods are declared private are not inherited.

Java Non-Access Modifiers

Java has a number of non-access modifiers to achieve other functionalities.
  • The static modifier is used for creating variables and class methods. 
  • The final modifier is used for finalizing the implementations of methods, classes, and variables. 
  • The abstract modifier is used for creating abstract classes and methods. 
  • The synchronized and volatile modifiers are used for threads.

The Static Modifier

Static Variables

The static variable is used as the static keyword for creating variables. It exists independently of any instances created for the class. The static variable exists regardless of the number of instances of the class.

Static variables are also called as class variables. But,  local variables are not declared as static.

Static Methods

The static keyword is used to creating static methods. It will independently of any instances created for the class.

Static methods do not use instance variables of an object of the class. Static methods take all the data from parameters and compute that parameter, without reference to variables.

Class variables and methods are accessed by the class name.

Java Static Methods

Java Static Methods

The Final Modifier

Final Variables

A final variable is explicitly initialized only once. The reference variable declared as final. It cannot reassign to the different object.

The data with the object it can be changed. The state of the object changed but cannot the reference.
The final modifier with variables is used with static to make as the constant the class variable.

Java Final Variables

Final Methods

 A final method cannot override by any other subclasses.

The main intention of the final method is that the content of the method not be changed by an outsider.

Java Final Methods

Final Classes

The main purpose of the final class is declared as final to prevent the class from subclassing. If the class is marked as final then the class cannot inherit any feature from the final class.

Java Final Classes

The Abstract Modifier

Abstract Class

An abstract class never instantiated. If any class is declared as abstract then the class to be extended.
A class cannot be abstract and final. If the class contains abstract methods then the class declared as abstract. Otherwise, a compile give the error.

An abstract class contains both abstract methods and normal methods.

Java Abstract Class

Abstract Methods

An abstract method is a method declared without any implementation. Abstract methods cannot be final or strict.

If any class extends an abstract class it implements all the abstract methods of the superclass, unless the subclass an abstract class.

If any class contains one or more abstract methods, and the class declared as abstract. An abstract class does not need to contain abstract methods.

An abstract method ends with the semicolon.

Example: public abstract sample();

Java Abstract Methods

The Synchronized Modifier

The synchronized keyword is used to indicate a method can access by only one thread. The synchronized modifier applied with four access level modifiers.

Java Synchronized Modifier

The Transient Modifier

An instance variable is transient to indicate the JVM to skip the particular variable when it serializing the object containing.

The modifier is included in the statement which creates the variable, preceding the class or data type of the variable.   

Java Transient Modifier

The Volatile Modifier

The volatile modifier is used to the JVM, that a thread accessing the variable.

Volatile only applied to the instance variables, which are of type object or private. A volatile object reference is null.

Java Volatile Modifier

run() method is called in one thread, and stop() method is called from another thread.
To use a modifier, need to include its volatile keyword in the definition of a class, method, or variable.
Java Volatile Modifier

Access Control Modifiers

Java has the number of access modifiers to access levels for classes, variables, methods, and constructors. Here is four access level:
  • It is visible to the package, the default. No modifiers are needed.
  • Visible to the private class only.
  • Visible to the public.
  • Visible to the package and all protected subclasses.

Read More

Saturday, 12 May 2018

Chaudhari Karan

Java variable types

By     No comments:

Lecture 5  Java – Variable Types

Hello, friend today we will discuss Variable types in java.

Java variable types

A variable provides named storage that our programs manipulate. In Java each variable has the specific type, that determines the layout and size of variable's memory; the range of values stored in that memory; the set of operations applied to the variable.

Here is an example of the basic form of variable declaration:

data type variable [ = value][, variable [ = value]… ]; 

In this example data type java's one data type and the variable is the name variable. The command-separated list is used to declare more than one variable of the specific type.

Here is an example of initialization and variable declaration.

Java variable types

Java has three types of variables. We will discuss in this chapter.
  • ·         Local variables
  • ·         Instance variables
  • ·         Class/Static  variables

Local Variable:

  • ·         Local variable declare in the constructors and methods.
  • ·         Access modifier is not used the local variable.
  • ·         Local variable creates when methods and constructor are entered and it destroyed when it exists in methods and constructor.
  • ·         The local variable is visible with constructor and methods.
  • ·         Local variable at stack level implemented.
  • ·         The local variable has no default value, so local variable declared and an initial value assigned before the first use.

Example: Here, age is the local variable. It is defined in pupAge() method.

Java variable types

Java variable types

Example: If age variable without initializing, so it would give error while compiling.

Java variable types

Program error:
Java variable types

Instance variable:

  • ·         Instance variable declared in a class. But its access outside the methods and constructor.
  • ·         In an instance, variable space allocated for the object, and for each variable value is created.
  • ·         Instance variable created by new keyword. It is created when an object is created and it is destroyed when destroyed.
  • ·         Instance variable hold value referenced by more than one constructor and methods. Objects states present throughout the class.
  • ·         Access modifier is given for instance variable.
  • ·         Instance variable declared in class level.
  • ·         Instance variable visible for all constructors, methods, and block in a class.
  • ·         Variable recommended making private variable. However visibility for subclasses given for variables with the access modifiers.
  • ·         The default value in an instance variable. For Boolean default value is false, for any number default value is o, and for object reference default value is null.
  • ·         Instance variable directly call by variable name inside the class. Example: .ObjectReferance.VariableName.

Java variable types
Java variable types

Class/static variable :

  • ·         The class variable also called the static variable. It is declared with static keyword in the class. But outside the methods.
  • ·         There is only one variable per class.
  • ·         A class variable is created when the program starts and it is destroyed when a program stops.
  • ·         Static variable rarely used than declared as constant. The constant variable declared as public, private, static and final. Constant variable never changes.
  • ·         Static variable store in static memory. It uses static variable other than final variable and it is used either public or private.
  • ·         Its visibility is similar to an instance variable.
  • ·         Static variable declared public since available for class.
  • ·         The static variable default value is same as an instance variable. For Boolean default value is false, for any number default value is o, and for object reference default value is null. The value assigned during declaration the constructor.
  • ·         Static variable call with its class name. Example: ClassName.VariableName.
  • ·         Declaring the class variable as public static final then variable name constant and all are in upper case. If static variable not final and public, the syntax name is same as local and instance variables.


Java variable types
Java variable types

Next: In next lecture, we will discuss modifier types in java.

Read More

Friday, 11 May 2018

Chaudhari Karan

Java basic datatypes

By     No comments:

Lecture 4  Java – Basic Datatypes

Hello friend, In this lecture, we will discuss Basic datatypes and Variable types.

Basic Datatypes:

Variable are reserved memory location to store data or value. Which means when we create the 
variable reserve some space in memory.

Data type based variable, the operating system allocates some memory and stored in reserved memory. Therefore, assigning different datatype to the variable, and it can be store decimals, integers, or character in these variables.

Two types of Datatypes in Java:

  • Primitive Datatypes
  • Object/Reference Datatypes

Primitive Datatypes:

Java has eight primitive data types. That is predefined by language and named by keyword. Now we  look eight primitive data types in detail.


  • Byte 8-bit two's complement integer data type
  • Byte minimum value is -128(-2^7)
  • Byte maximum value is 127(2^7-1)(Inclusive)
  • Default value is zero(0)
  • Byte is used for save space in large array, it is mainly place in integer, since byte is 4-times smaller than integer
  • Example: byte x = 10 , byte y = -20


  • Int is 32 two's complement integer data type
  • Int maximum value is  2,147,483,647(2^31-1)(Inclusive)
  • Int minimum value is  -2,147,483,648(-2^31)
  • Integer is used as default data type for integer values
  • Int default value is zero(0)
  • Example: int x = 200000, int y = -250000


  • Short is 16 two's complement integer data type
  • Short maximum value is 32,767(2^15-1)(Inclusive)
  • Short minimum value is -32,768(-2^15)
  • Short is used to save memory as byte data type. A short is two times smaller than an integer
  • Short default value is zero(0)
  • Example:  short x = 20000, short y = -30000


  • Double is the double-precision 64-bit IEEE 754 floating point data type
  • Double data type is used as default data type for decimal values, it is generally default choice
  • Double data type is never used for precise value like any currency($)
  • Its default value is 0.0d
  • Example: double d= 234.6


  • Float is the single-precision 32-bit IEEE 754 floating point data type
  • Float data type is save memory in arrays of floating point numbers
  • Its default value is 0.0f
  • Float data type is never store any precise value. Example, Currency($)
  • Example: float f = 123.6f


  • Long is 64-bit two complement integer  data type
  • Long maximum value is 9,223,372,036,854,775,807(2^63-1)(Inclusive)
  • Long minimum value is -9,223,372,036,854,775,808(-2^63)
  • Its default value is 0L
  • This type of data type is used for a wider range than int is needed
  • Example: long x = 200000L , long y = - 100000L


  • Char is single 16-bit Unicode character data type
  • Char is maximum value is 65,535(or ‘\uffff')
  • Char is minimum value is 0(or ‘\u0000')
  • Char is used for store any character
  • Example: char letterS = ‘S'


  • Boolean data type is represent one bit of information
  • Two value is available is Boolean:(1) True , (2)False
  • Its default value is false
  • Boolean is used for simple flags which track true/false condition
  • Example: boolean b = false

Object/Reference Datatype:

  • The reference variable is used to create the constructors of the classes. That are used to access objects. The variable is declared to the specific value which cannot be changed. Example: Student, Employee, etc.
  • Reference datatype default value is null.
  • Object class and different type of variable come under the reference datatype
  • A reference or object variable is used for refer to the object of the compatible type or declared type.
  • Example of reference datatype: Student student = new Student("James");

Java Literals

In Java, literal is the source code which represents of fixed value. It is represented directly in code without computation.

Literals assigned primitive data type variable. 


byte, long, int, and short expressed in octal(base 8), decimal(base 10), and hexadecimal(base 16) number system.

Prefix 0 is used for indicating octal, and prefix 0x is used to indicating hexadecimal number system. 


In Java, String literals are specified like in other languages enclosing a sequence character between the pair of double quotes. Here is an example of string literals :

char and String type of literals contain the Unicode character. 

In java support some special escape sequence for char and string literals.

Which is :
Character represented
Newline (0x0a)
Carriage return (0x0d)
Formfeed (0x0c)
Backspace (0x08)
Space (0x20)
Double quote
Single quote
Octal character (ddd)
Hexadecimal UNICODE character (xxxx)

Next: In next lecture, we will discuss Variable Types in Java.


Read More