SlideShare a Scribd company logo
1 of 22
Mobile App:IT
Eclipse Basics and Hello World
IHS GIRLS TECH CLUB: Java
Variables
WHAT ARE VARIABLES?
- Variables are core components in any program.
- A variable is a way to store information so you can reuse it
later.
- Without variables things like Microsoft Word, Facebook,
Amazon, and any other program or website, would not exist.
TWO PARTS OF A VARIABLE
- A variable contains two parts, a value, and a symbolic name.
- A value contains the information your variable is actually
storing
- A symbolic name or variable name is how you are able to set
and retrieve the value of the variable.
VARIABLES HAVE TYPES
- There are different types of variables.
- These types of variables are used to store different types of
data.
- Here are three common variable types.
- A string is used to store text information. An example would
be your name.
- An integer is used to store whole numbers. Examples include
1, 87, -24
- A double is used to store decimal numbers. Examples include
3.23, 8.7, -94.2
VARIABLE TYPES IN JAVA
- Java needs to know what type of information you plan on
storing in a variable.
- If you try to store the wrong type of information in a variable, a
Java program may crash, display errors, or work improperly.
VARIABLE EXAMPLES
- In the first example, a string variable is being created. The
variable name is str and the value is being set as "test".
COMMENTS
- You will notice that there are three lines of code in the
previous example starting with //
- These lines of code are called comments and will be ignored by
Java. This is useful for documenting or adding extra help text
to detail what specific sections of code do.
- Comments make it easier for you and other developers to
understand the Java program.
COMMENT EXAMPLES
- There are two ways to use comments. Single line comments, or
multi-line comments.
FOLLOW ALONG WITH ECLIPSE
- Now it is time to follow along with the code samples in the upcoming
slides.
- The first step is to open Eclipse and create a new project called
"Examples".
- After creating the project, Create a new Java class called Examples.
Make sure to check the box to add the "public static void main"
method.
- Inside the "main" method, you can type in the upcoming code and
run it after you finish typing in the code. The code should run
without any errors.
- Each time you type in the code, you should replace the code you
were previously working on.
- Make sure to keep all your code inside the main method.
PRINTING VARIABLES
- You already know how to print things using a line of code that
looks like:
- You can also print out variables. Here is an example of how to
do the same thing using variables. Notice how the variable
name is used inside println, instead of the text.
STRING VARIABLE EXAMPLES
- Now that example is not very useful, so we will look at how to
print your name using a variable.
- Using variables makes it simple to change the name that is
printed out.
- You will also notice we have introduced a new concept. Inside
the println method, we are adding two strings together. This
is called string concatenation.
ADDING VARIABLES
- Java makes it easy to add variables of the same type together.
VARIABLE MATH
- Java can also do math. Some basic examples are listed below.
PRINTING OUT TIME
CODE EXPLANATION
- In the previous example we introduce the print method.
- The print method can be called multiple times to build a line of
text to be output by your program.
- Like in the example, the series of print statements should
always end with a println method line.
VARIABLE TYPE TROUBLES
- In the previous example, you will notice that on the last line of
code we add together strings and integers. In this case it
works by automatically converting all the integers to strings
when the line of text is printed.
- Try the example below and examine the results.
MORE VARIABLE TYPE TROUBLES
- Try these examples and examine the results.
EXPLANATIONS AND CAUTIONS
- You will notice in the string and integer examples, things did not
always display as you might expect.
- What about the first division example? You would expect it to display
1.5 or perhaps round up to 2 but instead it outputs 1. This is
because you are dividing two integers. Because they are integers
they can not contain a decimal value, anything after the decimal
point is lost. This is why three divided by two with integers is
actually 1.
- As soon as you add one decimal point, Java knows that you want to
convert everything to a double. Because a double can store decimal
values, the math works correctly.
- Try out a few other examples and see what happens. As you can see,
variable types in Java are very important.
TRY THIS!
- Clear out all the code in your main method.
- Write a program that uses variables to store your name and
your age.
- Print out your name and age on one line.
- Print out how many months old you were at your last birthday
(hint: age * 12)
THE PROGRAM CODE (EXAMPLE 1)
THE PROGRAM CODE (EXAMPLE 2)
THE PROGRAM OUTPUT
- The results of the program should be similar to the text shown
below:

More Related Content

What's hot

Constructor in java
Constructor in javaConstructor in java
Constructor in javaHitesh Kumar
 
Multiple inheritance in java3 (1).pptx
Multiple inheritance in java3 (1).pptxMultiple inheritance in java3 (1).pptx
Multiple inheritance in java3 (1).pptxRkGupta83
 
Php Error Handling
Php Error HandlingPhp Error Handling
Php Error Handlingmussawir20
 
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)Michelle Anne Meralpis
 
Java variable types
Java variable typesJava variable types
Java variable typesSoba Arjun
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationHoneyChintal
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)arvind pandey
 
Java package
Java packageJava package
Java packageCS_GDRCST
 
Exception Handling
Exception HandlingException Handling
Exception HandlingReddhi Basu
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScriptShahDhruv21
 
Core java concepts
Core java  conceptsCore java  concepts
Core java conceptsRam132
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statementsKuppusamy P
 

What's hot (20)

Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Multiple inheritance in java3 (1).pptx
Multiple inheritance in java3 (1).pptxMultiple inheritance in java3 (1).pptx
Multiple inheritance in java3 (1).pptx
 
Java collections
Java collectionsJava collections
Java collections
 
Php Error Handling
Php Error HandlingPhp Error Handling
Php Error Handling
 
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)
 
Java Tokens
Java  TokensJava  Tokens
Java Tokens
 
Java variable types
Java variable typesJava variable types
Java variable types
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementation
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 
Java package
Java packageJava package
Java package
 
Features of java
Features of javaFeatures of java
Features of java
 
File handling
File handlingFile handling
File handling
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
 
Java program structure
Java program structureJava program structure
Java program structure
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statements
 
Generics in java
Generics in javaGenerics in java
Generics in java
 

Viewers also liked

Viewers also liked (20)

JVM, byte codes & jvm languages
JVM, byte codes & jvm languagesJVM, byte codes & jvm languages
JVM, byte codes & jvm languages
 
Why JVM will outlive java?
Why JVM will outlive java?Why JVM will outlive java?
Why JVM will outlive java?
 
Java basics variables
 Java basics   variables Java basics   variables
Java basics variables
 
Compilation of c
Compilation of cCompilation of c
Compilation of c
 
JVM
JVMJVM
JVM
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
4 java - decision
4  java - decision4  java - decision
4 java - decision
 
Java virtual machine
Java virtual machineJava virtual machine
Java virtual machine
 
01 introduction to oop and java
01 introduction to oop and java01 introduction to oop and java
01 introduction to oop and java
 
Programming Fundamentals With OOPs Concepts (Java Examples Based)
Programming Fundamentals With OOPs Concepts (Java Examples Based)Programming Fundamentals With OOPs Concepts (Java Examples Based)
Programming Fundamentals With OOPs Concepts (Java Examples Based)
 
3 java - variable type
3  java - variable type3  java - variable type
3 java - variable type
 
Java Basic Oops Concept
Java Basic Oops ConceptJava Basic Oops Concept
Java Basic Oops Concept
 
Oops in java
Oops in javaOops in java
Oops in java
 
Java OOP Programming language (Part 1) - Introduction to Java
Java OOP Programming language (Part 1) - Introduction to JavaJava OOP Programming language (Part 1) - Introduction to Java
Java OOP Programming language (Part 1) - Introduction to Java
 
OOPs in Java
OOPs in JavaOOPs in Java
OOPs in Java
 
Java String
Java String Java String
Java String
 
String Handling
String HandlingString Handling
String Handling
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Oop java
Oop javaOop java
Oop java
 
OOP java
OOP javaOOP java
OOP java
 

Similar to 07 java variables

Clean code - DSC DYPCOE
Clean code - DSC DYPCOEClean code - DSC DYPCOE
Clean code - DSC DYPCOEPatil Shreyas
 
Basics of Programming - A Review Guide
Basics of Programming - A Review GuideBasics of Programming - A Review Guide
Basics of Programming - A Review GuideBenjamin Kissinger
 
Cis 1403 lab1- the process of programming
Cis 1403 lab1- the process of programmingCis 1403 lab1- the process of programming
Cis 1403 lab1- the process of programmingHamad Odhabi
 
Standard coding practices
Standard coding practicesStandard coding practices
Standard coding practicesAnilkumar Patil
 
Modern_2.pptx for java
Modern_2.pptx for java Modern_2.pptx for java
Modern_2.pptx for java MayaTofik
 
Java syntax-and-grammars-oct8
Java syntax-and-grammars-oct8Java syntax-and-grammars-oct8
Java syntax-and-grammars-oct8MISSIASABTAL1
 
The Expression Problem - Part 2
The Expression Problem - Part 2The Expression Problem - Part 2
The Expression Problem - Part 2Philip Schwarz
 
Dependency Injection for PHP
Dependency Injection for PHPDependency Injection for PHP
Dependency Injection for PHPmtoppa
 
C programming perso notes
C programming perso notesC programming perso notes
C programming perso notesMelanie Tsopze
 
E learning excel vba programming lesson 3
E learning excel vba programming  lesson 3E learning excel vba programming  lesson 3
E learning excel vba programming lesson 3Vijay Perepa
 
Lab 1 Recursion  Introduction   Tracery (tracery.io.docx
Lab 1 Recursion  Introduction   Tracery (tracery.io.docxLab 1 Recursion  Introduction   Tracery (tracery.io.docx
Lab 1 Recursion  Introduction   Tracery (tracery.io.docxsmile790243
 
[Pick the date] Class Instructor’s NameWhat Is Autism.docx
[Pick the date]  Class  Instructor’s NameWhat Is Autism.docx[Pick the date]  Class  Instructor’s NameWhat Is Autism.docx
[Pick the date] Class Instructor’s NameWhat Is Autism.docxdanielfoster65629
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaHamad Odhabi
 
Visual Programming
Visual ProgrammingVisual Programming
Visual ProgrammingBagzzz
 
13 javascript techniques to improve your code
13 javascript techniques to improve your code13 javascript techniques to improve your code
13 javascript techniques to improve your codeSurendra kumar
 

Similar to 07 java variables (20)

06 java methods
06 java methods06 java methods
06 java methods
 
Clean code - DSC DYPCOE
Clean code - DSC DYPCOEClean code - DSC DYPCOE
Clean code - DSC DYPCOE
 
Computer programming 2 chapter 1
Computer programming 2  chapter 1Computer programming 2  chapter 1
Computer programming 2 chapter 1
 
L05 Design Patterns
L05 Design PatternsL05 Design Patterns
L05 Design Patterns
 
Basics of Programming - A Review Guide
Basics of Programming - A Review GuideBasics of Programming - A Review Guide
Basics of Programming - A Review Guide
 
L03 Design Patterns
L03 Design PatternsL03 Design Patterns
L03 Design Patterns
 
Cis 1403 lab1- the process of programming
Cis 1403 lab1- the process of programmingCis 1403 lab1- the process of programming
Cis 1403 lab1- the process of programming
 
Standard coding practices
Standard coding practicesStandard coding practices
Standard coding practices
 
C question
C questionC question
C question
 
Modern_2.pptx for java
Modern_2.pptx for java Modern_2.pptx for java
Modern_2.pptx for java
 
Java syntax-and-grammars-oct8
Java syntax-and-grammars-oct8Java syntax-and-grammars-oct8
Java syntax-and-grammars-oct8
 
The Expression Problem - Part 2
The Expression Problem - Part 2The Expression Problem - Part 2
The Expression Problem - Part 2
 
Dependency Injection for PHP
Dependency Injection for PHPDependency Injection for PHP
Dependency Injection for PHP
 
C programming perso notes
C programming perso notesC programming perso notes
C programming perso notes
 
E learning excel vba programming lesson 3
E learning excel vba programming  lesson 3E learning excel vba programming  lesson 3
E learning excel vba programming lesson 3
 
Lab 1 Recursion  Introduction   Tracery (tracery.io.docx
Lab 1 Recursion  Introduction   Tracery (tracery.io.docxLab 1 Recursion  Introduction   Tracery (tracery.io.docx
Lab 1 Recursion  Introduction   Tracery (tracery.io.docx
 
[Pick the date] Class Instructor’s NameWhat Is Autism.docx
[Pick the date]  Class  Instructor’s NameWhat Is Autism.docx[Pick the date]  Class  Instructor’s NameWhat Is Autism.docx
[Pick the date] Class Instructor’s NameWhat Is Autism.docx
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in Java
 
Visual Programming
Visual ProgrammingVisual Programming
Visual Programming
 
13 javascript techniques to improve your code
13 javascript techniques to improve your code13 javascript techniques to improve your code
13 javascript techniques to improve your code
 

Recently uploaded

OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 

Recently uploaded (20)

OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 

07 java variables

  • 1. Mobile App:IT Eclipse Basics and Hello World IHS GIRLS TECH CLUB: Java Variables
  • 2. WHAT ARE VARIABLES? - Variables are core components in any program. - A variable is a way to store information so you can reuse it later. - Without variables things like Microsoft Word, Facebook, Amazon, and any other program or website, would not exist.
  • 3. TWO PARTS OF A VARIABLE - A variable contains two parts, a value, and a symbolic name. - A value contains the information your variable is actually storing - A symbolic name or variable name is how you are able to set and retrieve the value of the variable.
  • 4. VARIABLES HAVE TYPES - There are different types of variables. - These types of variables are used to store different types of data. - Here are three common variable types. - A string is used to store text information. An example would be your name. - An integer is used to store whole numbers. Examples include 1, 87, -24 - A double is used to store decimal numbers. Examples include 3.23, 8.7, -94.2
  • 5. VARIABLE TYPES IN JAVA - Java needs to know what type of information you plan on storing in a variable. - If you try to store the wrong type of information in a variable, a Java program may crash, display errors, or work improperly.
  • 6. VARIABLE EXAMPLES - In the first example, a string variable is being created. The variable name is str and the value is being set as "test".
  • 7. COMMENTS - You will notice that there are three lines of code in the previous example starting with // - These lines of code are called comments and will be ignored by Java. This is useful for documenting or adding extra help text to detail what specific sections of code do. - Comments make it easier for you and other developers to understand the Java program.
  • 8. COMMENT EXAMPLES - There are two ways to use comments. Single line comments, or multi-line comments.
  • 9. FOLLOW ALONG WITH ECLIPSE - Now it is time to follow along with the code samples in the upcoming slides. - The first step is to open Eclipse and create a new project called "Examples". - After creating the project, Create a new Java class called Examples. Make sure to check the box to add the "public static void main" method. - Inside the "main" method, you can type in the upcoming code and run it after you finish typing in the code. The code should run without any errors. - Each time you type in the code, you should replace the code you were previously working on. - Make sure to keep all your code inside the main method.
  • 10. PRINTING VARIABLES - You already know how to print things using a line of code that looks like: - You can also print out variables. Here is an example of how to do the same thing using variables. Notice how the variable name is used inside println, instead of the text.
  • 11. STRING VARIABLE EXAMPLES - Now that example is not very useful, so we will look at how to print your name using a variable. - Using variables makes it simple to change the name that is printed out. - You will also notice we have introduced a new concept. Inside the println method, we are adding two strings together. This is called string concatenation.
  • 12. ADDING VARIABLES - Java makes it easy to add variables of the same type together.
  • 13. VARIABLE MATH - Java can also do math. Some basic examples are listed below.
  • 15. CODE EXPLANATION - In the previous example we introduce the print method. - The print method can be called multiple times to build a line of text to be output by your program. - Like in the example, the series of print statements should always end with a println method line.
  • 16. VARIABLE TYPE TROUBLES - In the previous example, you will notice that on the last line of code we add together strings and integers. In this case it works by automatically converting all the integers to strings when the line of text is printed. - Try the example below and examine the results.
  • 17. MORE VARIABLE TYPE TROUBLES - Try these examples and examine the results.
  • 18. EXPLANATIONS AND CAUTIONS - You will notice in the string and integer examples, things did not always display as you might expect. - What about the first division example? You would expect it to display 1.5 or perhaps round up to 2 but instead it outputs 1. This is because you are dividing two integers. Because they are integers they can not contain a decimal value, anything after the decimal point is lost. This is why three divided by two with integers is actually 1. - As soon as you add one decimal point, Java knows that you want to convert everything to a double. Because a double can store decimal values, the math works correctly. - Try out a few other examples and see what happens. As you can see, variable types in Java are very important.
  • 19. TRY THIS! - Clear out all the code in your main method. - Write a program that uses variables to store your name and your age. - Print out your name and age on one line. - Print out how many months old you were at your last birthday (hint: age * 12)
  • 20. THE PROGRAM CODE (EXAMPLE 1)
  • 21. THE PROGRAM CODE (EXAMPLE 2)
  • 22. THE PROGRAM OUTPUT - The results of the program should be similar to the text shown below: