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

Javaaaaa@a

Uploaded by

hindhuindhu8
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Javaaaaa@a

Uploaded by

hindhuindhu8
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Datatypes

Presented by
Team-4
Data Types in
Java:
Definition:
Data types in Java define the type of data that
can be stored in variables. They specify the size and
type of values that can be held in variables, helping
to allocate the correct amount of memory.
Purpose:
To specify the kind of data a variable can hold
(e.g., integer, decimal, text, etc.).
To optimize memory usage and ensure type
safety (e.g., preventing operations between
incompatible data types).
Simple term’s:
Types of Data types:

Types of Data Types in Java:


1. Primitive Data Types:Predefined by the
language.Store simple values directly in
memory.Examples: int, float, char, boolean.
2. Reference Data Types:Store references
(addresses) to objects.Used for more
complex data structures.Examples: String,
Arrays, Classes
Primitive Data
types:
1.Byte
Definition:8-bit integer. Stores small integer
values.
Range: -128 to 127
Example:byte age = 25;
2. Short
Definition:16-bit integer. Stores slightly larger
integer values.
Range: -32,768 to 32,767
Example:short distance = 15000;
Primitive Data
Types(contd.)
3. intDefinition: 32-bit integer, Default data
type for integers.
Range: -2^31 to 2^31-1
Example:int population = 1000000;
4. longDefinition: 64-bit integer,Used for
large integer values
Range: -2^63 to 2^63-1
Example:long worldPopulation
=7800000000L;
Primitive Data Types
(contd.)
5. float
Definition: 32-bit floating point. Stores decimal values.
Example:
float temperature = 36.6f;
6. double
Definition: 64-bit floating point. More precise for
decimal values.
Example:
double pi = 3.14159;
Primitive Data Types
(contd.)
7. char
Definition: 16-bit Unicode character, Stores a
single character.
Example:
char grade = ‘A’;
8. boolean
Definition: Stores only two values: true or false.
Example:
boolean isJavaFun = true;
Reference Data
Types
1.StringDefinition: Stores sequences of
characters (text).
Example:String message = "Hello,
Java!";
2. ArrayDefinition:
Stores multiple values of the same data
type in a single variable.
Example:int[] numbers = {1, 2, 3};
Reference Data Types
3. ClassDefinition: (contd.)
A blueprint for creating objects.
Classes hold methods variables.
Example:class Car { String model =
"Sedan";}Car myCar = new Car();
4. InterfaceDefinition:
Specifies what methods a class must
implement but not how.
Example:interface Vehicle { void start();}
Java
Variables
*A variable is the name of a reserved area
allocated in memory.
* In other words, it is a name of the memory
location.
* It is a combination of "vary + able" which
means its value can be changed.
Example:

int data=50;//Here data is variable


Types of Variables
There are three types of variables
in Java:
* local variable
* instance variable
* static variable
Types of Variables(continued)
2) Instance
1) Local Variable Variable

A variable declared A variable declared


inside the body of the inside the class but
method is called local outside the body of the
variable. You can use method, is called an
this variable only within instance variable. It is
that method and the not declared
other methods in the as static.It is called an
class aren't even aware instance variable
that the variable because its value is
exists.A local variable instance-specific and is
cannot be defined with not shared among
"static" keyword. instances
3) Static variable

A variable that is declared as static is called a static variable.


It cannot be local.
You can create a single copy of the static variable and share it
among all the instances of the class.
Memory allocation for static variables happens only once
when the class is loaded in the memory.
Example the types of variables in
java

public class A
{
static int m=100;//static variable
void method()
{
int n=90;//local variable
}
public static void main(String args[])
{
int data=50;//instance variable
}
}//end of class
Java Variable Example: Add
Two Numbers
public class Simple{
public static void main(String[] args){
int a=10;
int b=10;
int c=a+b;
System.out.println(c);
}
}

OUTPUT:
20
Java Constant
• Constant is a value that cannot be changed after
assigning it.
• Java does not directly support the constants.
• There is an alternative way to define the constants
in Java by using the non-access modifiers static and
final.
• In Java, to declare any variable as constant, we
use static and final modifiers.
• It is also known as non-access modifiers. According
to the Java naming convention the identifier name
must be in capital letters.
Static and Final Modifiers

• The purpose to use the static modifier is


to manage the memory.
• It also allows the variable to be available
without loading any instance of the class
in which it is defined.
• The final modifier represents that the
value of the variable cannot be changed.
It also makes the primitive data type
immutable or unchangeable.
Syntax :
static final datatype identifier_name=value;

• For example, price is a variable that we want to


make constant.
• static final double PRICE=432.78;
• Where static and final are the non-access
modifiers. The double is the data type and PRICE is
the identifier name in which the value 432.78 is
assigned.
• In the above statement, the static modifier causes
the variable to be available without an instance of
its defining class being loaded and
the final modifier makes the variable fixed.
Use of
constants:
The use of constants in programming
makes the program easy and
understandable which can be easily
understood by others.
 It also affects the performance
because a constant variable is cached
by both JVM and the application.
Points to
Remember:
• Write the identifier name in capital letters
that we want to declare as constant. For
example, MAX=12.
• If we use the private access-specifier
before the constant name, the value of
the constant cannot be changed in that
particular class.
• If we use the public access-specifier
before the constant name, the value of
the constant can be changed in the
program.
Example : Declaring Constant as
Private
ConstantExample.java
import java.util.Scanner;
public class ConstantExample1
{
//declaring constant
private static final double PRICE=234.90;
public static void main(String[] args)
{
int unit;
double total_bill;
System.out.print("Enter the number of units you have used: ")
;
Scanner sc=new Scanner(System.in);
unit=sc.nextInt();
total_bill=PRICE*unit;
System.out.println("The total amount you have to deposit is: "
+total_bill);
}
}
OUTPUT:

Explanation
A class with the main() method to determine the
total cost based on a fixed price per unit is defined
by the Java program ConstantExample. To guarantee
its immutability, it declares a constant PRICE with
the value 234.90 by employing the last keyword.
CONCLUSION:

Definition: Variables store mutable data, while constants


hold immutable values, enhancing code clarity.
Data Types: Different data types (e.g., integers, strings, booleans)
dictate how data is processed and stored.
Type Safety: Ensures operations are performed on compatible
data types,reducing runtime errors.
Scope & Lifetime: Variables and constants have defined scopes
and lifetimes,affecting their accessibility and memory usage.
Efficiency: Proper use of variables, constants, and data types
optimizes,performance and resource management in programming.

You might also like