Data Type in Java
Data Type in Java
Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float
and double.
Primitive data are only single values, they have not special capabilities.
Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.
A non-primitive data type is something else such as an array structure or class is known
as the non-primitive data type.
public static void main (String [] args) Java main method is the entry point of any java
program. Its syntax is always public static void main (String [] args) You can only change the
name of String array argument, for example you can change args to myStringArgs.
Static: It is a particular type of method. Static methods can be called without creation of the
objects inside the class. In general, you cannot call any method without an object. In Java there is
a rule that, every method should be called on the object.
void: This means nothing. It has significance that Java compiler isn't interested in the return type
of this method. As every method has a specific return type, so in this case, main method does not
return anything. Hence it returns nothing or in other words avoid it.
String [] args: This is the array of type string and its name is ‘args’. This can take the inputs
from the command line in the string format. we can replace the array name with anything then
also it works fine. i.e. String [] xyz or String abc [] etc.
Java is known as platform independent because it uses the concept of generating the byte code
of the high level program and then run that byte code (intermediate code) on JVM (Java Virtual
Machine), actual JVM is a virtual computer system that run on your original computer system.
JVM is an abstract machine. It is a specification that provides runtime environment in which java
bytecode can be executed. A Java virtual machine is a virtual machine that enables a computer to
run Java programs as well as programs written in other languages that are also compiled to Java
bytecode. The JVM is detailed by a specification that formally describes what is required of a
JVM implementation.
1. Variable Declaration
2. Variable Initialization
Variable Declaration:
To declare a variable, you must specify the data type & give the variable a unique name.
int a,b,c;
float pi;
double d;
char a;
Variable Initialization:
pi= 3-14f;
do =20.20d;
a= ‘v’;
Example:
int a=2,b=4,c=6;
float pi=3.14;
double do=20.22;
char a=’v’;
Local variables
Instance variables
Class/Static variables
Local Variables
A variable that is declared within the method that is called local variables. It is defined in method or
other statements, such as defined and used within the cache block, and outside the block or method,
the variable cannot be used.
Instance variables
A non-static variable that is declared within the class but not in the method is called instance
variable. Instance variables are related to a specific object; they can access class variables.
Class/Static variables
A variable that is declared with static keyword in a class but not in the method is called static or class
variable.
Example 1
class A {
Example 2
salary = 10000;
eg.show();
}
Difference between JDK, JRE, and JVM
1. Java Virtual Machine JVM
2. Java Runtime Environment (JRE)
3. Java Development Kit (JDK)
We must understand the differences between JDK, JRE, and JVM before proceeding further
to Java. See the brief overview of JVM here.
If you want to get the detailed knowledge of Java Virtual Machine, move to the next page.
Firstly, let's see the differences between the JDK, JRE, and JVM.
JVM
JVM (Java Virtual Machine) is an abstract machine. It is called a virtual machine because it
doesn't physically exist. It is a specification that provides a runtime environment in which Java
bytecode can be executed. It can also run those programs which are written in other languages
and compiled to Java bytecode.
VMs are available for many hardware and software platforms. JVM, JRE, and JDK are platform
dependent because the configuration of each OS is different from each other. However, Java is
platform independent. There are three notions of the JVM: specification, implementation,
and instance.
The JVM performs the following main tasks:
o Loads code
o Verifies code
o Executes code
o Provides runtime environment
JRE
JRE is an acronym for Java Runtime Environment. It is also written as Java RTE. The Java
Runtime Environment is a set of software tools which are used for developing Java applications.
It is used to provide the runtime environment. It is the implementation of JVM. It physically
exists. It contains a set of libraries + other files that JVM uses at runtime.
The implementation of JVM is also actively released by other companies besides Sun Micro
Systems.
JDK
JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is a software
development environment which is used to develop Java applications and applets. It physically
exists. It contains JRE + development tools.
JDK is an implementation of any one of the below given Java Platforms released by Oracle
Corporation:
o Standard Edition Java Platform
o Enterprise Edition Java Platform
o Micro Edition Java Platform
The JDK contains a private Java Virtual Machine (JVM) and a few other resources such as an
interpreter/loader (java), a compiler (javac), an archiver (jar), a documentation generator
(Javadoc), etc. to complete the development of a Java Application.
Programming
All programmes consist of two elements: code and data. Furthermore, a programme can be
conceptually organised around its code or around its data.
That is, some programmes are written around “what is happening” and others are written around “who
is being affected “. These are the two paradigms that govern how a programme is constructed. The first
way is called the process-oriented model. This approach characterizes a programme as a series of linear
steps (that is code). Process oriented model can be thought of as code acting on data. Problem with this
approach appear as program grow longer and more complex.
To manage increasing complexity. The second approach, called object-oriented programming (OOP) was
conceived. Object-oriented programming organizes a programme around the data (that is objects) and a
set of well –defined interfaces to that data. An object-oriented programme can be characterized as data
controlling access to code.
Inheritance
Inheritance is the process by which one object acquires the properties of another object.
Inheritance is the idea that an entity can inherit attributes from another entity. It allows the
programmer to create similar entities without needing to redefine similar attributes over and
over.
Inheritance is a feature of object-oriented programming that allows code reusability when a
class includes property of another class. Considering Human Being a class, which has properties
like hands, legs, eyes, mouth, etc, and functions like walk, talk, eat, see etc.
Polymorphism
Polymorphism (from the Greek, meaning “many forms) is a feature that allows one interface to be used
for a general class of action. More generally, the concept of polymorphism is often expressed by the
phrase “one interface, many methods” This means that it is possible to design a generic interface to a
group of activities. This helps reduce complexity by allowing the same interface to be used to specify a
general class of action. It is the compiler’s job to select the specific action (that is method) as it applies
to each situation.
package primenumber1;
int i =0;
int counter=0;
if(i%num==0)
counter = counter + 1;
if (counter ==2)
}
}
System.out.println(primeNumbers);
//Output:
//2 3 5 7 11 13 17 19 23 29 31 37 41 43 47