SlideShare uma empresa Scribd logo
1 de 21
Data Types and
Variables
What are DataTypes?
 Data-types means containers.
 They specify what kind of data to hold
 In java, there are two types of data types
 Primitive data types
 Non-primitive data types
Primitive Data Types
Non Primitive Data Types
 Strings
 Arrays
 Class
Variables
 Variable is name of reserved area allocated in memory.
 There are three types of variables in java
 local variable
 A variable that is declared inside the method is called local variable.
 instance variable
 A variable that is declared inside the class but outside the method is
called instance variable . It is not declared as static.
 static variable
 A variable that is declared as static is called static variable. It cannot
be local.
Variables
Syntax:
Datatype variable_name = value ;
Example
int id = 34;
float mks=67.34;
int int =45; //invalid
Literals
 Literals are numbers, characters, and string representations used
in a program.
 Numeric Literals
int roll_no=45;
 Character literals
char letter = ‘b';
letter = ‘S';
 String literals
String message = "HellotWorldn";
Casting is required when there is a need to explicitly convert a
value from one type to another.
The general syntax for a cast is:
Examples
Casting
(result_type) value;
float price = 37.53;
int dollars = (int) price; // fractional portion lost
// dollars = 37
char response = 'A';
byte temp =(byte) response; // temp = 65 (ASCII value
// for 'a')
Type Casting
 Assigning a value of one type to a variable of another
type is known as Type Casting.
 In Java, type casting is classified into two types,
 Widening Casting(Implicit)
 Narrowing Casting(Explicitly done)
A widening conversion occurs when a value stored in a
smaller space is converted to a type of a larger space.
There will never be a loss of information
Widening conversions occur automatically when needed.
Widening Conversions
A narrowing conversion occurs when a value stored in a larger
space is converted to a type of a smaller space.
Information may be lost
Never occurs automatically. Must be explicitly requested by
the programmer using a cast.
Narrowing Conversions
Strings
 String is a sequence of characters. In the Java programming
language, strings are objects. There are two types of String
i.e
 1. Mutable
 2.Immutable
 In java, string objects are immutable.
 Immutable simply means unmodifiable or unchangeable. It
is immutable because object reference is created.
String Formatting
StringBuffer Class
 Java StringBuffer class is used to created mutable
(modifiable) string. The StringBuffer class in java is same as
String class except it is mutable i.e. it can be changed.
class StringDemo{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello ");
sb.append("Java");//now original string is changed
System.out.println(sb);//prints Hello Java
}
}
StringBuilder Class
 Java StringBuilder class is used to create mutable (modifiable) string.
 The Java StringBuilder class is same as StringBuffer class except that it
is non-synchronized.
class A{
public static void main(String args[]){
StringBuilder sb=new StringBuilder("Hello ");
sb.append("Java");//now original string is changed
System.out.println(sb);//prints Hello Java
}
}
Difference between String and
StringBuffer
String
 String class is immutable
 String is slow and
consumes more memory
 String class overrides the
equals() method of
Object class.
StringBuffer
 StringBuffer class is
mutable
 StringBuffer is fast and
consumes less memory
 StringBuffer class doesn't
override the equals()
method of Object class
Difference between String and
StringBuffer conti..
StringBuffer
 StringBuffer
is synchronized
 StringBuffer is less
efficient than
StringBuilder.
StringBuilder
 StringBuilder is non-
synchronized
 StringBuilder is more
efficient than
StringBuffer.
Arrays
 An array is an ordered list of values
0 1 2 3 4 5 6 7 8 9
79 87 94 82 67 98 87 81 74 91
An array of size N is indexed from zero to N-1
rollno
The entire array
has a single name
Each value has a numeric index
This array holds 10 values that are indexed from 0 to 9
Array - Syntax
 A particular value in an array is referenced using the array
name followed by the index in brackets.
 Datatype [ ] variable_name = new datatype[size];
 Ex: int [] scores = new int [10];
 In JAVA, int is of 4 bytes, total space=4*10=40 bytes.
 Space Allocation is based on the datatype used to declare
array.
Types of Array
 Single Dimensional Array (1 D Array)
 Two Dimensional Array (2 D Array)
 Multidimensional Array(Arrays of Arrays)
Dot Operator
 Dot operator is used to access methods and variables
within classes.
 It is represented as . (dot)
Example
class Test
{
public int a;
public display()
{
System.out.println(“Hello”);
}
}
public static void main(String args[])
{
Test t=new Test();
t.a=32; //accessing dot
t.display(); //accessing display
}
}

Mais conteúdo relacionado

Mais procurados

Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 

Mais procurados (20)

Constructor overloading & method overloading
Constructor overloading & method overloadingConstructor overloading & method overloading
Constructor overloading & method overloading
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Java program structure
Java program structure Java program structure
Java program structure
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 
Data types
Data typesData types
Data types
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Wrapper classes
Wrapper classesWrapper classes
Wrapper classes
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Java and its features
Java and its featuresJava and its features
Java and its features
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Java Presentation For Syntax
Java Presentation For SyntaxJava Presentation For Syntax
Java Presentation For Syntax
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and Operators
 
Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Constants in java
Constants in javaConstants in java
Constants in java
 

Semelhante a Data Types & Variables in JAVA

javastringexample problems using string class
javastringexample problems using string classjavastringexample problems using string class
javastringexample problems using string class
fedcoordinator
 

Semelhante a Data Types & Variables in JAVA (20)

javastringexample problems using string class
javastringexample problems using string classjavastringexample problems using string class
javastringexample problems using string class
 
Java string handling
Java string handlingJava string handling
Java string handling
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Java - Basic Datatypes.pptx
Java - Basic Datatypes.pptxJava - Basic Datatypes.pptx
Java - Basic Datatypes.pptx
 
Fileoperations.pptx
Fileoperations.pptxFileoperations.pptx
Fileoperations.pptx
 
Java String
Java String Java String
Java String
 
In the given example only one object will be created. Firstly JVM will not fi...
In the given example only one object will be created. Firstly JVM will not fi...In the given example only one object will be created. Firstly JVM will not fi...
In the given example only one object will be created. Firstly JVM will not fi...
 
Array.ppt
Array.pptArray.ppt
Array.ppt
 
array.ppt
array.pptarray.ppt
array.ppt
 
Java data types, variables and jvm
Java data types, variables and jvm Java data types, variables and jvm
Java data types, variables and jvm
 
Java unit 2
Java unit 2Java unit 2
Java unit 2
 
OCA Java SE 8 Exam Chapter 3 Core Java APIs
OCA Java SE 8 Exam Chapter 3 Core Java APIsOCA Java SE 8 Exam Chapter 3 Core Java APIs
OCA Java SE 8 Exam Chapter 3 Core Java APIs
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Java
JavaJava
Java
 
Module 6 - String Manipulation.pdf
Module 6 - String Manipulation.pdfModule 6 - String Manipulation.pdf
Module 6 - String Manipulation.pdf
 
JVM and OOPS Introduction
JVM and OOPS IntroductionJVM and OOPS Introduction
JVM and OOPS Introduction
 
Wrapper classes
Wrapper classesWrapper classes
Wrapper classes
 
Learning core java
Learning core javaLearning core java
Learning core java
 
JAVA_1.pptx
JAVA_1.pptxJAVA_1.pptx
JAVA_1.pptx
 
Java
JavaJava
Java
 

Último

Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 

Último (20)

Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLEGEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech Civil
 

Data Types & Variables in JAVA

  • 2. What are DataTypes?  Data-types means containers.  They specify what kind of data to hold  In java, there are two types of data types  Primitive data types  Non-primitive data types
  • 4. Non Primitive Data Types  Strings  Arrays  Class
  • 5. Variables  Variable is name of reserved area allocated in memory.  There are three types of variables in java  local variable  A variable that is declared inside the method is called local variable.  instance variable  A variable that is declared inside the class but outside the method is called instance variable . It is not declared as static.  static variable  A variable that is declared as static is called static variable. It cannot be local.
  • 6. Variables Syntax: Datatype variable_name = value ; Example int id = 34; float mks=67.34; int int =45; //invalid
  • 7. Literals  Literals are numbers, characters, and string representations used in a program.  Numeric Literals int roll_no=45;  Character literals char letter = ‘b'; letter = ‘S';  String literals String message = "HellotWorldn";
  • 8. Casting is required when there is a need to explicitly convert a value from one type to another. The general syntax for a cast is: Examples Casting (result_type) value; float price = 37.53; int dollars = (int) price; // fractional portion lost // dollars = 37 char response = 'A'; byte temp =(byte) response; // temp = 65 (ASCII value // for 'a')
  • 9. Type Casting  Assigning a value of one type to a variable of another type is known as Type Casting.  In Java, type casting is classified into two types,  Widening Casting(Implicit)  Narrowing Casting(Explicitly done)
  • 10. A widening conversion occurs when a value stored in a smaller space is converted to a type of a larger space. There will never be a loss of information Widening conversions occur automatically when needed. Widening Conversions
  • 11. A narrowing conversion occurs when a value stored in a larger space is converted to a type of a smaller space. Information may be lost Never occurs automatically. Must be explicitly requested by the programmer using a cast. Narrowing Conversions
  • 12. Strings  String is a sequence of characters. In the Java programming language, strings are objects. There are two types of String i.e  1. Mutable  2.Immutable  In java, string objects are immutable.  Immutable simply means unmodifiable or unchangeable. It is immutable because object reference is created.
  • 14. StringBuffer Class  Java StringBuffer class is used to created mutable (modifiable) string. The StringBuffer class in java is same as String class except it is mutable i.e. it can be changed. class StringDemo{ public static void main(String args[]){ StringBuffer sb=new StringBuffer("Hello "); sb.append("Java");//now original string is changed System.out.println(sb);//prints Hello Java } }
  • 15. StringBuilder Class  Java StringBuilder class is used to create mutable (modifiable) string.  The Java StringBuilder class is same as StringBuffer class except that it is non-synchronized. class A{ public static void main(String args[]){ StringBuilder sb=new StringBuilder("Hello "); sb.append("Java");//now original string is changed System.out.println(sb);//prints Hello Java } }
  • 16. Difference between String and StringBuffer String  String class is immutable  String is slow and consumes more memory  String class overrides the equals() method of Object class. StringBuffer  StringBuffer class is mutable  StringBuffer is fast and consumes less memory  StringBuffer class doesn't override the equals() method of Object class
  • 17. Difference between String and StringBuffer conti.. StringBuffer  StringBuffer is synchronized  StringBuffer is less efficient than StringBuilder. StringBuilder  StringBuilder is non- synchronized  StringBuilder is more efficient than StringBuffer.
  • 18. Arrays  An array is an ordered list of values 0 1 2 3 4 5 6 7 8 9 79 87 94 82 67 98 87 81 74 91 An array of size N is indexed from zero to N-1 rollno The entire array has a single name Each value has a numeric index This array holds 10 values that are indexed from 0 to 9
  • 19. Array - Syntax  A particular value in an array is referenced using the array name followed by the index in brackets.  Datatype [ ] variable_name = new datatype[size];  Ex: int [] scores = new int [10];  In JAVA, int is of 4 bytes, total space=4*10=40 bytes.  Space Allocation is based on the datatype used to declare array.
  • 20. Types of Array  Single Dimensional Array (1 D Array)  Two Dimensional Array (2 D Array)  Multidimensional Array(Arrays of Arrays)
  • 21. Dot Operator  Dot operator is used to access methods and variables within classes.  It is represented as . (dot) Example class Test { public int a; public display() { System.out.println(“Hello”); } } public static void main(String args[]) { Test t=new Test(); t.a=32; //accessing dot t.display(); //accessing display } }