SlideShare une entreprise Scribd logo
1  sur  17
Declaration and Initialization of Variables




                                              1
Objectives

On completion of this period, you would be able to
 know :

•   To declare variables
•   To initialize variables




                                                     2
Recap

•   Classify data types in Java

•   What is a literal?

•   How many types of literals are in Java?




                                              3
Declaration of Variables

•   In Java, variables are the names of storage
    locations
•   After designing variables,we must declare them




                                                     4
Contd..
           Declaration of Variables

•   Declaration does three things
•   It tells the compiler what the variable name is
•   It specifies what type of data the variable will hold
•   The place of declaration decides the scope of the
    variable



                                                           5
Contd..
            Declaration of Variables

•   The syntax of declaration of a variable is

•   type variable1,variable2,variable3,…,variableN;


•   Here, type specifies the data type of which kind
    and variables are separated by commas
•   A declaration statement ends with a semicolon
                    eg. : int d, m;

                                                                6
Initialization of Variables

•   A variable must be given a value after it has been
    declared but before it is used in an expression
•   This can be done in two ways
       •   By using an assignment statement
       •   By using a read statement




                                                     7
By Using an Assignment Statement

•   A simple method of giving values to a variable is
    through the assignment statement as follows


        variable_name = value;
        eg. : n=9,p=18;




                                                        8
Contd..
      By Using an Assignment Statement

•   It is also possible to assign a value to a variable at
    the time of it’s declaration this in the form of
      type variable_name = value;
•   The process of giving initial values to variables is
    known as ‘Initialization’




                                                             9
By Using Read Statement

•   We may also give values to variables to the
    keyboard using the readLine( ) method
    eg.      import java.io.DataInputStream;
               class Example
                {
                    public static void main(String args[])
                     {
                          DataInputStream in = new
                            DataInputStream(System.in);
                              int d=0;



                                                             10
Contd..
           By Using Read Statement
double m=0.0
try
{
   system.out.println(“ Enter any integer value”);
                          d = Integer. parseInt
   ( in.readLine());
   system. out. println (“enter any double value”);
                            m = Double. valueOf
   ( in.readLine() ).doubleValue();
}




                                                                11
Contd..
        By Using Read Statement
    catch (Exception e)
    {
    }
    System .out. println (“The integer value is”+d);
    System.out.println(“The double value is”+m);
             }
}




                                                            12
Contd..
          By Using Read Statement
•   The readLine() method reads the input from the
    keyboard as a string and is converted to the
    corresponding data type using the data type


•   The keywords try and catch used to handle any
    exceptions that occur during the reading process




                                                        13
Summary

•   In this class we have discussed declaration and
    initialization of variables




                                                      14
Quiz


1.Initialization of a variable can be done by using
a)    an assignment operator
b)    a read statement
c)    both of the above




                                                      15
Frequently Asked Questions


1.   Explain how a variable can be initialized.

2.   Show how a variable can be declared




                                                  16
swings
Struts
jdbc
hibernate
home
java previous question papers
 OCT/NOV-2012 QUESTION PAPER
April / May 2012 c-09
October/ November-2011 c-09
April/ May 2011 c-09      
April/ May 2011 c-05
                          

                                17

Contenu connexe

Tendances

C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01
Zafor Iqbal
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Anton Kasyanov, Introduction to Python, Lecture5
Anton Kasyanov, Introduction to Python, Lecture5Anton Kasyanov, Introduction to Python, Lecture5
Anton Kasyanov, Introduction to Python, Lecture5
Anton Kasyanov
 

Tendances (20)

Chapter 2 - Getting Started with Java
Chapter 2 - Getting Started with JavaChapter 2 - Getting Started with Java
Chapter 2 - Getting Started with Java
 
Chapter 8 - Exceptions and Assertions Edit summary
Chapter 8 - Exceptions and Assertions  Edit summaryChapter 8 - Exceptions and Assertions  Edit summary
Chapter 8 - Exceptions and Assertions Edit summary
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and Strings
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Javapolymorphism
JavapolymorphismJavapolymorphism
Javapolymorphism
 
Introduction to c ++ part -2
Introduction to c ++   part -2Introduction to c ++   part -2
Introduction to c ++ part -2
 
C++ classes
C++ classesC++ classes
C++ classes
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part II
 
Data types, Variables, Expressions & Arithmetic Operators in java
Data types, Variables, Expressions & Arithmetic Operators in javaData types, Variables, Expressions & Arithmetic Operators in java
Data types, Variables, Expressions & Arithmetic Operators in java
 
C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01
 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part I
 
Ppt on java basics
Ppt on java basicsPpt on java basics
Ppt on java basics
 
Class and object
Class and objectClass and object
Class and object
 
Basic concept of class, method , command line-argument
Basic concept of class, method , command line-argumentBasic concept of class, method , command line-argument
Basic concept of class, method , command line-argument
 
Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interface
 
Anton Kasyanov, Introduction to Python, Lecture5
Anton Kasyanov, Introduction to Python, Lecture5Anton Kasyanov, Introduction to Python, Lecture5
Anton Kasyanov, Introduction to Python, Lecture5
 
Std 12 computer chapter 8 classes and object in java (part 2)
Std 12 computer chapter 8 classes and object in java (part 2)Std 12 computer chapter 8 classes and object in java (part 2)
Std 12 computer chapter 8 classes and object in java (part 2)
 
Pointers
PointersPointers
Pointers
 

En vedette

Programming Concepts 01
Programming Concepts 01Programming Concepts 01
Programming Concepts 01
Learn 2 Be
 

En vedette (12)

Programming Concepts 01
Programming Concepts 01Programming Concepts 01
Programming Concepts 01
 
Introduction to programming
Introduction to programmingIntroduction to programming
Introduction to programming
 
Intro to Adobe Flash
Intro to Adobe FlashIntro to Adobe Flash
Intro to Adobe Flash
 
Introduction flash
Introduction flashIntroduction flash
Introduction flash
 
Lecture 1 introduction to flash
Lecture 1 introduction to flashLecture 1 introduction to flash
Lecture 1 introduction to flash
 
Introduction To Flash
Introduction To FlashIntroduction To Flash
Introduction To Flash
 
Adobe Flash History and Basics
Adobe Flash History and BasicsAdobe Flash History and Basics
Adobe Flash History and Basics
 
Object oriented programming (oop) cs304 power point slides lecture 01
Object oriented programming (oop)   cs304 power point slides lecture 01Object oriented programming (oop)   cs304 power point slides lecture 01
Object oriented programming (oop) cs304 power point slides lecture 01
 
Animation
AnimationAnimation
Animation
 
Introduction to Animation
Introduction to AnimationIntroduction to Animation
Introduction to Animation
 
Creating Animated PPT presentations
Creating Animated PPT presentationsCreating Animated PPT presentations
Creating Animated PPT presentations
 
ANIMATION PPT
ANIMATION PPTANIMATION PPT
ANIMATION PPT
 

Similaire à Variables 9 cm604.7

Java basics variables
 Java basics   variables Java basics   variables
Java basics variables
JoeReddieMedia
 
02. Data Type and Variables
02. Data Type and Variables02. Data Type and Variables
02. Data Type and Variables
Tommy Vercety
 
5. using variables, data, expressions and constants
5. using variables, data, expressions and constants5. using variables, data, expressions and constants
5. using variables, data, expressions and constants
CtOlaf
 
JAVA in Artificial intelligent
JAVA in Artificial intelligentJAVA in Artificial intelligent
JAVA in Artificial intelligent
Virat Andodariya
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4th
Connex
 
Presentation 2nd
Presentation 2ndPresentation 2nd
Presentation 2nd
Connex
 
Presentation 5th
Presentation 5thPresentation 5th
Presentation 5th
Connex
 
Android webinar class_java_review
Android webinar class_java_reviewAndroid webinar class_java_review
Android webinar class_java_review
Edureka!
 

Similaire à Variables 9 cm604.7 (20)

Java basics variables
 Java basics   variables Java basics   variables
Java basics variables
 
02. Data Type and Variables
02. Data Type and Variables02. Data Type and Variables
02. Data Type and Variables
 
Lec-2- Ehsjdjkck. Jdkdbd djskrogramming.pdf
Lec-2- Ehsjdjkck. Jdkdbd djskrogramming.pdfLec-2- Ehsjdjkck. Jdkdbd djskrogramming.pdf
Lec-2- Ehsjdjkck. Jdkdbd djskrogramming.pdf
 
Full CSE 310 Unit 1 PPT.pptx for java language
Full CSE 310 Unit 1 PPT.pptx for java languageFull CSE 310 Unit 1 PPT.pptx for java language
Full CSE 310 Unit 1 PPT.pptx for java language
 
5. using variables, data, expressions and constants
5. using variables, data, expressions and constants5. using variables, data, expressions and constants
5. using variables, data, expressions and constants
 
Md03 - part3
Md03 - part3Md03 - part3
Md03 - part3
 
CSC111-Chap_02.pdf
CSC111-Chap_02.pdfCSC111-Chap_02.pdf
CSC111-Chap_02.pdf
 
java in Aartificial intelligent by virat andodariya
java in Aartificial intelligent by virat andodariyajava in Aartificial intelligent by virat andodariya
java in Aartificial intelligent by virat andodariya
 
JAVA in Artificial intelligent
JAVA in Artificial intelligentJAVA in Artificial intelligent
JAVA in Artificial intelligent
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4th
 
2.Getting Started with C#.Net-(C#)
2.Getting Started with C#.Net-(C#)2.Getting Started with C#.Net-(C#)
2.Getting Started with C#.Net-(C#)
 
9 cm604.9
9 cm604.99 cm604.9
9 cm604.9
 
Presentation 2nd
Presentation 2ndPresentation 2nd
Presentation 2nd
 
intro_java (1).pptx
intro_java (1).pptxintro_java (1).pptx
intro_java (1).pptx
 
Presentation 5th
Presentation 5thPresentation 5th
Presentation 5th
 
Android webinar class_java_review
Android webinar class_java_reviewAndroid webinar class_java_review
Android webinar class_java_review
 
Ch 2 Library Classes.pptx
Ch 2 Library Classes.pptxCh 2 Library Classes.pptx
Ch 2 Library Classes.pptx
 
Ch 2 Library Classes.pdf
Ch 2 Library Classes.pdfCh 2 Library Classes.pdf
Ch 2 Library Classes.pdf
 
Learn To Code: Diving deep into java
Learn To Code: Diving deep into javaLearn To Code: Diving deep into java
Learn To Code: Diving deep into java
 
C# - Part 1
C# - Part 1C# - Part 1
C# - Part 1
 

Plus de myrajendra (20)

Fundamentals
FundamentalsFundamentals
Fundamentals
 
Data type
Data typeData type
Data type
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc drivers
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1
 
Dao example
Dao exampleDao example
Dao example
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
 
Internal
InternalInternal
Internal
 
3. elements
3. elements3. elements
3. elements
 
2. attributes
2. attributes2. attributes
2. attributes
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
 
Headings
HeadingsHeadings
Headings
 
Forms
FormsForms
Forms
 
Css
CssCss
Css
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
 

Variables 9 cm604.7

  • 2. Objectives On completion of this period, you would be able to know : • To declare variables • To initialize variables 2
  • 3. Recap • Classify data types in Java • What is a literal? • How many types of literals are in Java? 3
  • 4. Declaration of Variables • In Java, variables are the names of storage locations • After designing variables,we must declare them 4
  • 5. Contd.. Declaration of Variables • Declaration does three things • It tells the compiler what the variable name is • It specifies what type of data the variable will hold • The place of declaration decides the scope of the variable 5
  • 6. Contd.. Declaration of Variables • The syntax of declaration of a variable is • type variable1,variable2,variable3,…,variableN; • Here, type specifies the data type of which kind and variables are separated by commas • A declaration statement ends with a semicolon eg. : int d, m; 6
  • 7. Initialization of Variables • A variable must be given a value after it has been declared but before it is used in an expression • This can be done in two ways • By using an assignment statement • By using a read statement 7
  • 8. By Using an Assignment Statement • A simple method of giving values to a variable is through the assignment statement as follows variable_name = value; eg. : n=9,p=18; 8
  • 9. Contd.. By Using an Assignment Statement • It is also possible to assign a value to a variable at the time of it’s declaration this in the form of type variable_name = value; • The process of giving initial values to variables is known as ‘Initialization’ 9
  • 10. By Using Read Statement • We may also give values to variables to the keyboard using the readLine( ) method eg. import java.io.DataInputStream; class Example { public static void main(String args[]) { DataInputStream in = new DataInputStream(System.in); int d=0; 10
  • 11. Contd.. By Using Read Statement double m=0.0 try { system.out.println(“ Enter any integer value”); d = Integer. parseInt ( in.readLine()); system. out. println (“enter any double value”); m = Double. valueOf ( in.readLine() ).doubleValue(); } 11
  • 12. Contd.. By Using Read Statement catch (Exception e) { } System .out. println (“The integer value is”+d); System.out.println(“The double value is”+m); } } 12
  • 13. Contd.. By Using Read Statement • The readLine() method reads the input from the keyboard as a string and is converted to the corresponding data type using the data type • The keywords try and catch used to handle any exceptions that occur during the reading process 13
  • 14. Summary • In this class we have discussed declaration and initialization of variables 14
  • 15. Quiz 1.Initialization of a variable can be done by using a) an assignment operator b) a read statement c) both of the above 15
  • 16. Frequently Asked Questions 1. Explain how a variable can be initialized. 2. Show how a variable can be declared 16
  • 17. swings Struts jdbc hibernate home java previous question papers  OCT/NOV-2012 QUESTION PAPER April / May 2012 c-09 October/ November-2011 c-09 April/ May 2011 c-09   April/ May 2011 c-05   17