0% found this document useful (0 votes)
30 views

Data Type in Java

Uploaded by

Roo Anu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Data Type in Java

Uploaded by

Roo Anu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Java Programming

There are two types of data types 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.

List of Java's primitive data types


Type Size in Bytes Range
Integer
byte 1 byte -128 to 127
short 2 bytes -32,768 to 32,767
int 4 bytes -2,147,483,648 to 2,147,483, 647
long 8 bytes -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
Real Numbers
float 4 bytes approximately ±3.40282347E+38F
(6-7 significant decimal digits)
Java implements IEEE 754 standard
double 8 bytes approximately ±1.79769313486231570E+308
(15 significant decimal digits)
Others
char 2 byte 0 to 65,536 (unsigned)
boolean not precisely defined* true or false

Structure of java programme


A class, in the context of Java, are templates that are used to create objects, and to define object
data types and methods. Core properties include the data types and methods that may be used by
the object. All class objects should have the basic class properties.

Defining a Class.: A class--the basic building block of an object-oriented language such


as Java--is a template that describes the data and behavior associated with instances of
that class. When you instantiate a class you create an object that looks and feels like
other instances of the same class.

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.

The meaning of all terms are as follows:


public: This is the access specifier of the method. It indicates that this method can be used in any
class inside any package. Generally, there are private and protected types are also available.
These are having the limitations to be called on the objects.

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.

Variable in Java Programming


A variable can be thought of as a container which holds value for you, during the life of a Java
program. Every variable is assigned a data type which designates the type and quantity of value
it can hold.

In order to use a variable in a program you to need to perform 2 steps

1. Variable Declaration
2. Variable Initialization

Rules of declaring variable in java


1. A variable name can consist of Capital letters A-Z, lowercase letters a-z, digits 0-9, and
two special characters such as underscore and dollar Sign.
2. The first character must be a letter.
3. Blank spaces cannot be used in variable names.
4. Java keywords cannot be used as variable names.
5. Variable names are case-sensitive.

Variable Declaration:

To declare a variable, you must specify the data type & give the variable a unique name.

Examples of other Valid Declarations are

int a,b,c;

float pi;

double d;

char a;

Variable Initialization:

To initialize a variable, you must assign it a valid value.


Count =100;

Example of other Valid Initializations are

pi= 3-14f;

do =20.20d;

a= ‘v’;

You can combine variable declaration and initialization.


int count =100;

Example:

int a=2,b=4,c=6;

float pi=3.14;

double do=20.22;

char a=’v’;

Scope of variable in Java


Variable Scope means - That limit, as far as the variable can be used.
In Java there are various types of variable scope:

 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 {

int amount = 100; //instance variable

static int pin = 2315; //static variable

public static void main(String[] args) {

int age = 35; //local variable

Example 2

public class Example {

int salary; // Instance Variable

public void show() {

int value = 2; // Local variable

value = value + 10;

System.out.println("The Value is : " + value);

salary = 10000;

System.out.println("The salary is : " + salary);

public static void main(String args[]) {

Example eg = new Example();

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.

Object -Oriented Programme


Object Oriented programming is a programming style which is associated with the concepts like
class, object, Inheritance, Encapsulation, Abstraction, Polymorphism.
Abstraction
An essential element of object- oriented programming is abstraction. Human manage complexity
through abstraction. A powerful way to manage abstraction is through the use of hierarchical
classification. From the outside, the car is a single object once inside you see that the car consists of
several subsystems. These subsystems are made up of more specialised units. To manage the complexity
of the car through the use of hierarchical abstractions.
Hierarchical abstractions of complex systems can also be applied to computer programmes. The data
from a traditional process-oriented programme can be transformed by abstraction into its component
objects. A sequence of process steps can become a collection of messages between these objects. Thus,
each of these objects describes its own unique behaviour. You can treat these objects as concrete
entities that respond to manages telling them to do something. This is the essence of object-oriented
programming.
The three OOP Principles
All object-oriented programing languages provide mechanisms that help you implement the object-
oriented model. They are encapsulation, inheritance and polymorphism.
Encapsulation
Encapsulation is the mechanism that binds together code and the data it manipulates and keeps both
safe from outside interference and misuse. In java the basis of encapsulation is the class. Encapsulation
means that we want to hide unnecessary details from the user.

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.

Find Prime numbers

package primenumber1;

public class Primenumber1

public static void main (String[] args)

int i =0;

int num =0;

String primeNumbers = "";

for (i = 1; i <= 50; i++)

int counter=0;

for(num =i; num>=1; num--)

if(i%num==0)

counter = counter + 1;

if (counter ==2)

primeNumbers = primeNumbers + i + " ";

}
}

System.out.println("Prime numbers from 1 to 50 are :");

System.out.println(primeNumbers);

//Output:

//Prime numbers from 1 to 50 are :

//2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

You might also like