SlideShare a Scribd company logo
1 of 17
IMPLEMENTING ASSIGNMENT
STATEMENTS AND EXPRESSION
Chapter 3.2:
Assignment Statement
 An assignment statement changes the value
of a variable
 The assignment operator is the = sign
 Syntax for assignment statement:
variable = expression;
• E.g:
• The value that was in total is overwritten
• You can only assign a value to a variable that
is consistent with the variable's declared type
• See Geometry.java
total = 55;
Assignment Statement
//********************************************************************
// Geometry.java Author: Lewis/Loftus
//
// Demonstrates the use of an assignment statement to change the
// value stored in a variable.
//********************************************************************
public class Geometry
{
//-----------------------------------------------------------------
// Prints the number of sides of several geometric shapes.
//-----------------------------------------------------------------
public static void main (String[] args)
{
int sides = 7; // declaration with initialization
System.out.println ("A heptagon has " + sides + " sides.");
sides = 10; // assignment statement
System.out.println ("A decagon has " + sides + " sides.");
sides = 12;
System.out.println ("A dodecagon has " + sides + " sides.");
}
}
Assignment Statement
//********************************************************************
// Geometry.java Author: Lewis/Loftus
//
// Demonstrates the use of an assignment statement to change the
// value stored in a variable.
//********************************************************************
public class Geometry
{
//-----------------------------------------------------------------
// Prints the number of sides of several geometric shapes.
//-----------------------------------------------------------------
public static void main (String[] args)
{
int sides = 7; // declaration with initialization
System.out.println ("A heptagon has " + sides + " sides.");
sides = 10; // assignment statement
System.out.println ("A decagon has " + sides + " sides.");
sides = 12;
System.out.println ("A dodecagon has " + sides + " sides.");
}
}
Output
A heptagon has 7 sides.
A decagon has 10 sides.
a dodecagon has 12 sides.
Declaring, Initializing and
Assignment
j=k=x=2; equivalent to: x=2;
k=x;
j=k;
 Declare and initialize. E.g:
int x = 1;
int i =1, j=2;
Uninitialized Variables
 Error:
An Uninitialized Object
Variable
int luckyNumber;
System.out.println(luckyNumber);
// ERROR - uninitialized variable
Constants
 A constant is an identifier that is similar to a
variable except that it holds the same value during
its entire existence
 As the name implies, it is constant, not variable
 The compiler will issue an error if you try to
change the value of a constant
Constants
 Permanent data that never change during
program execution
 Example:  = 3.14159
 Declared by using the reserved word final.
 Initialized when it is declared.
 Syntax:
final datatype CONSTANTNAME = value
 Example :
final double CENTIMETERS_PER_INCH = 2.54;
final int NO_OF_STUDENTS = 20;
final char BLANK = ' ';
final double PAY_RATE = 15.75;
Data Types
 A set of values together with a set of operations.
Primitive Data Types
• The primitive data type are the fundamental data
types in Java.
Primitive Data Types
• Integral data types (deals with integers, or numbers
without a decimal part):
Primitive Data Types
 Floating-point data types (deals with
decimal numbers):
 float
 double
 boolean (deals with logical values):
 True
 False
Values and Memory Allocation for
Integral and Floating-point Data
Types
Characters
 A char variable stores a single character
 Character literals are delimited by single
quotes:
'a' 'X' '7' '$' ',' 'n'
 Example declarations:
char topGrade = 'A';
char terminator = ';', separator = ' ';
 Note the difference between a primitive
character variable, which holds only one
character, and a String object, which can
hold multiple characters
Characters
 A character set is an ordered list of characters,
with each character corresponding to a unique
number
 A char variable in Java can store any character
from the Unicode character set
 The Unicode character set uses sixteen bits per
character, allowing for 65,536 unique characters
 It is an international character set, containing
symbols and characters from many world
languages
Characters
 The ASCII character set is older and smaller than
Unicode, but is still quite popular
 The ASCII characters are a subset of the Unicode
character set, including:
uppercase letters
lowercase letters
punctuation
digits
special symbols
control characters
A, B, C, …
a, b, c, …
period, semi-colon, …
0, 1, 2, …
&, |, , …
carriage return, tab, ...
Boolean
 A boolean value represents a true or false
condition
 The reserved words true and false are the
only valid values for a boolean type
boolean done = false;
 A boolean variable can also be used to represent
any two states, such as a light bulb being on or off

More Related Content

What's hot

Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
Varun C M
 
Fundamental programming structures in java
Fundamental programming structures in javaFundamental programming structures in java
Fundamental programming structures in java
Shashwat Shriparv
 
PHP Unit 3 functions_in_php_2
PHP Unit 3 functions_in_php_2PHP Unit 3 functions_in_php_2
PHP Unit 3 functions_in_php_2
Kumar
 
Javascript sivasoft
Javascript sivasoftJavascript sivasoft
Javascript sivasoft
ch samaram
 
Lecture 23
Lecture 23Lecture 23
Lecture 23
rhshriva
 

What's hot (19)

Effective PHP. Part 3
Effective PHP. Part 3Effective PHP. Part 3
Effective PHP. Part 3
 
Regular expressions in Perl
Regular expressions in PerlRegular expressions in Perl
Regular expressions in Perl
 
Effective PHP. Part 5
Effective PHP. Part 5Effective PHP. Part 5
Effective PHP. Part 5
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
 
Fundamental programming structures in java
Fundamental programming structures in javaFundamental programming structures in java
Fundamental programming structures in java
 
JavaScript Tutorial
JavaScript  TutorialJavaScript  Tutorial
JavaScript Tutorial
 
Perl names values and variables
Perl names values and variablesPerl names values and variables
Perl names values and variables
 
Unit 2.5
Unit 2.5Unit 2.5
Unit 2.5
 
Effective PHP. Part 2
Effective PHP. Part 2Effective PHP. Part 2
Effective PHP. Part 2
 
Comma operator
Comma operatorComma operator
Comma operator
 
PHP Unit 3 functions_in_php_2
PHP Unit 3 functions_in_php_2PHP Unit 3 functions_in_php_2
PHP Unit 3 functions_in_php_2
 
Java Script Introduction
Java Script IntroductionJava Script Introduction
Java Script Introduction
 
JavaScript 101 - Class 1
JavaScript 101 - Class 1JavaScript 101 - Class 1
JavaScript 101 - Class 1
 
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
JavaScript: The Good Parts Or: How A C# Developer Learned To Stop Worrying An...
 
5 Statements and Control Structures
5 Statements and Control Structures5 Statements and Control Structures
5 Statements and Control Structures
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
Javascript sivasoft
Javascript sivasoftJavascript sivasoft
Javascript sivasoft
 
Compiler: Programming Language= Assignments and statements
Compiler: Programming Language= Assignments and statementsCompiler: Programming Language= Assignments and statements
Compiler: Programming Language= Assignments and statements
 
Lecture 23
Lecture 23Lecture 23
Lecture 23
 

Viewers also liked

Cyberbullying film 260
Cyberbullying film 260Cyberbullying film 260
Cyberbullying film 260
teaganmclaren
 
Chapter 6.3
Chapter 6.3Chapter 6.3
Chapter 6.3
sotlsoc
 
Chapter 4.0
Chapter 4.0Chapter 4.0
Chapter 4.0
sotlsoc
 
Final flipbook presentation
Final flipbook presentationFinal flipbook presentation
Final flipbook presentation
Taryn Pilon
 
Chapter 9.3
Chapter 9.3Chapter 9.3
Chapter 9.3
sotlsoc
 
Chapter 10.2
Chapter 10.2Chapter 10.2
Chapter 10.2
sotlsoc
 
Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6
sotlsoc
 
Chapter 4.3
Chapter 4.3Chapter 4.3
Chapter 4.3
sotlsoc
 
Chapter 8.1
Chapter 8.1Chapter 8.1
Chapter 8.1
sotlsoc
 
Chapter 7.2
Chapter 7.2Chapter 7.2
Chapter 7.2
sotlsoc
 
정몽준 서울시장 새누리당 후보 정책 공약 _ 33한서울88한경제
정몽준 서울시장 새누리당 후보 정책 공약 _ 33한서울88한경제 정몽준 서울시장 새누리당 후보 정책 공약 _ 33한서울88한경제
정몽준 서울시장 새누리당 후보 정책 공약 _ 33한서울88한경제
MJeNews
 
Chapter 11.1
Chapter 11.1Chapter 11.1
Chapter 11.1
sotlsoc
 
Chapter 11.3
Chapter 11.3Chapter 11.3
Chapter 11.3
sotlsoc
 
Presentacion leydi perezI
Presentacion leydi perezIPresentacion leydi perezI
Presentacion leydi perezI
leydip2009
 
Dangers to social media
Dangers to social mediaDangers to social media
Dangers to social media
Ryan Ward
 

Viewers also liked (18)

Cyberbullying film 260
Cyberbullying film 260Cyberbullying film 260
Cyberbullying film 260
 
Chapter 6.3
Chapter 6.3Chapter 6.3
Chapter 6.3
 
Chapter 4.0
Chapter 4.0Chapter 4.0
Chapter 4.0
 
Final flipbook presentation
Final flipbook presentationFinal flipbook presentation
Final flipbook presentation
 
Chapter 9.3
Chapter 9.3Chapter 9.3
Chapter 9.3
 
Chapter 10.2
Chapter 10.2Chapter 10.2
Chapter 10.2
 
e-Sports
e-Sportse-Sports
e-Sports
 
Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6
 
Chapter 4.3
Chapter 4.3Chapter 4.3
Chapter 4.3
 
Chapter 8.1
Chapter 8.1Chapter 8.1
Chapter 8.1
 
Chapter 7.2
Chapter 7.2Chapter 7.2
Chapter 7.2
 
정몽준 서울시장 새누리당 후보 정책 공약 _ 33한서울88한경제
정몽준 서울시장 새누리당 후보 정책 공약 _ 33한서울88한경제 정몽준 서울시장 새누리당 후보 정책 공약 _ 33한서울88한경제
정몽준 서울시장 새누리당 후보 정책 공약 _ 33한서울88한경제
 
Chapter 11.1
Chapter 11.1Chapter 11.1
Chapter 11.1
 
Chapter 11.3
Chapter 11.3Chapter 11.3
Chapter 11.3
 
Cyberbullying by alex pitcher
Cyberbullying by alex pitcherCyberbullying by alex pitcher
Cyberbullying by alex pitcher
 
Presentacion leydi perezI
Presentacion leydi perezIPresentacion leydi perezI
Presentacion leydi perezI
 
Dangers to social media
Dangers to social mediaDangers to social media
Dangers to social media
 
Smart phone. addiction. blindness
Smart phone. addiction. blindnessSmart phone. addiction. blindness
Smart phone. addiction. blindness
 

Similar to Chapter 3.2

Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
GANESHBABUVelu
 
Gnu octave help book 02 of 02
Gnu octave help book 02 of 02Gnu octave help book 02 of 02
Gnu octave help book 02 of 02
Arun Umrao
 

Similar to Chapter 3.2 (20)

Java basics and java variables
Java basics and java variablesJava basics and java variables
Java basics and java variables
 
Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++
 
C# language basics (Visual Studio)
C# language basics (Visual Studio) C# language basics (Visual Studio)
C# language basics (Visual Studio)
 
C# language basics (Visual studio)
C# language basics (Visual studio)C# language basics (Visual studio)
C# language basics (Visual studio)
 
Java: Primitive Data Types
Java: Primitive Data TypesJava: Primitive Data Types
Java: Primitive Data Types
 
Java Programming
Java Programming Java Programming
Java Programming
 
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
 
Esoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingEsoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programming
 
Lecture2_MCS4_Evening.pptx
Lecture2_MCS4_Evening.pptxLecture2_MCS4_Evening.pptx
Lecture2_MCS4_Evening.pptx
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
 
java tutorial 2
 java tutorial 2 java tutorial 2
java tutorial 2
 
Introduction to-programming
Introduction to-programmingIntroduction to-programming
Introduction to-programming
 
Md03 - part3
Md03 - part3Md03 - part3
Md03 - part3
 
Identifiers, keywords and types
Identifiers, keywords and typesIdentifiers, keywords and types
Identifiers, keywords and types
 
vb.net.pdf
vb.net.pdfvb.net.pdf
vb.net.pdf
 
unit 1 (1).pptx
unit 1 (1).pptxunit 1 (1).pptx
unit 1 (1).pptx
 
JavaScript.pptx
JavaScript.pptxJavaScript.pptx
JavaScript.pptx
 
Lecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops conceptLecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops concept
 
Gnu octave help book 02 of 02
Gnu octave help book 02 of 02Gnu octave help book 02 of 02
Gnu octave help book 02 of 02
 
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
 

More from sotlsoc

Chapter 2.0 new
Chapter 2.0 newChapter 2.0 new
Chapter 2.0 new
sotlsoc
 
Chapter 1.0 new
Chapter 1.0 newChapter 1.0 new
Chapter 1.0 new
sotlsoc
 
Chapter 3.0 new
Chapter 3.0 newChapter 3.0 new
Chapter 3.0 new
sotlsoc
 
Chapter 6.5 new
Chapter 6.5 newChapter 6.5 new
Chapter 6.5 new
sotlsoc
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3
sotlsoc
 
Chapter 11.4
Chapter 11.4Chapter 11.4
Chapter 11.4
sotlsoc
 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5
sotlsoc
 
Chapter 11.2
Chapter 11.2Chapter 11.2
Chapter 11.2
sotlsoc
 
Chapter 11.0
Chapter 11.0Chapter 11.0
Chapter 11.0
sotlsoc
 
Chapter 10.1
Chapter 10.1Chapter 10.1
Chapter 10.1
sotlsoc
 
Chapter 8.0
Chapter 8.0Chapter 8.0
Chapter 8.0
sotlsoc
 
Chapter 10.0
Chapter 10.0Chapter 10.0
Chapter 10.0
sotlsoc
 
Chapter 9.4
Chapter 9.4Chapter 9.4
Chapter 9.4
sotlsoc
 
Chapter 9.1
Chapter 9.1Chapter 9.1
Chapter 9.1
sotlsoc
 
Chapter 9.0
Chapter 9.0Chapter 9.0
Chapter 9.0
sotlsoc
 
Chapter 9.2
Chapter 9.2Chapter 9.2
Chapter 9.2
sotlsoc
 
Chapter 8.3
Chapter 8.3Chapter 8.3
Chapter 8.3
sotlsoc
 
Chapter 8.2
Chapter 8.2Chapter 8.2
Chapter 8.2
sotlsoc
 
Chapter 5.2
Chapter 5.2Chapter 5.2
Chapter 5.2
sotlsoc
 
Chapter 7.4
Chapter 7.4Chapter 7.4
Chapter 7.4
sotlsoc
 

More from sotlsoc (20)

Chapter 2.0 new
Chapter 2.0 newChapter 2.0 new
Chapter 2.0 new
 
Chapter 1.0 new
Chapter 1.0 newChapter 1.0 new
Chapter 1.0 new
 
Chapter 3.0 new
Chapter 3.0 newChapter 3.0 new
Chapter 3.0 new
 
Chapter 6.5 new
Chapter 6.5 newChapter 6.5 new
Chapter 6.5 new
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3
 
Chapter 11.4
Chapter 11.4Chapter 11.4
Chapter 11.4
 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5
 
Chapter 11.2
Chapter 11.2Chapter 11.2
Chapter 11.2
 
Chapter 11.0
Chapter 11.0Chapter 11.0
Chapter 11.0
 
Chapter 10.1
Chapter 10.1Chapter 10.1
Chapter 10.1
 
Chapter 8.0
Chapter 8.0Chapter 8.0
Chapter 8.0
 
Chapter 10.0
Chapter 10.0Chapter 10.0
Chapter 10.0
 
Chapter 9.4
Chapter 9.4Chapter 9.4
Chapter 9.4
 
Chapter 9.1
Chapter 9.1Chapter 9.1
Chapter 9.1
 
Chapter 9.0
Chapter 9.0Chapter 9.0
Chapter 9.0
 
Chapter 9.2
Chapter 9.2Chapter 9.2
Chapter 9.2
 
Chapter 8.3
Chapter 8.3Chapter 8.3
Chapter 8.3
 
Chapter 8.2
Chapter 8.2Chapter 8.2
Chapter 8.2
 
Chapter 5.2
Chapter 5.2Chapter 5.2
Chapter 5.2
 
Chapter 7.4
Chapter 7.4Chapter 7.4
Chapter 7.4
 

Recently uploaded

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Recently uploaded (20)

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 

Chapter 3.2

  • 1. IMPLEMENTING ASSIGNMENT STATEMENTS AND EXPRESSION Chapter 3.2:
  • 2. Assignment Statement  An assignment statement changes the value of a variable  The assignment operator is the = sign  Syntax for assignment statement: variable = expression; • E.g: • The value that was in total is overwritten • You can only assign a value to a variable that is consistent with the variable's declared type • See Geometry.java total = 55;
  • 3. Assignment Statement //******************************************************************** // Geometry.java Author: Lewis/Loftus // // Demonstrates the use of an assignment statement to change the // value stored in a variable. //******************************************************************** public class Geometry { //----------------------------------------------------------------- // Prints the number of sides of several geometric shapes. //----------------------------------------------------------------- public static void main (String[] args) { int sides = 7; // declaration with initialization System.out.println ("A heptagon has " + sides + " sides."); sides = 10; // assignment statement System.out.println ("A decagon has " + sides + " sides."); sides = 12; System.out.println ("A dodecagon has " + sides + " sides."); } }
  • 4. Assignment Statement //******************************************************************** // Geometry.java Author: Lewis/Loftus // // Demonstrates the use of an assignment statement to change the // value stored in a variable. //******************************************************************** public class Geometry { //----------------------------------------------------------------- // Prints the number of sides of several geometric shapes. //----------------------------------------------------------------- public static void main (String[] args) { int sides = 7; // declaration with initialization System.out.println ("A heptagon has " + sides + " sides."); sides = 10; // assignment statement System.out.println ("A decagon has " + sides + " sides."); sides = 12; System.out.println ("A dodecagon has " + sides + " sides."); } } Output A heptagon has 7 sides. A decagon has 10 sides. a dodecagon has 12 sides.
  • 5. Declaring, Initializing and Assignment j=k=x=2; equivalent to: x=2; k=x; j=k;  Declare and initialize. E.g: int x = 1; int i =1, j=2;
  • 6. Uninitialized Variables  Error: An Uninitialized Object Variable int luckyNumber; System.out.println(luckyNumber); // ERROR - uninitialized variable
  • 7. Constants  A constant is an identifier that is similar to a variable except that it holds the same value during its entire existence  As the name implies, it is constant, not variable  The compiler will issue an error if you try to change the value of a constant
  • 8. Constants  Permanent data that never change during program execution  Example:  = 3.14159  Declared by using the reserved word final.  Initialized when it is declared.  Syntax: final datatype CONSTANTNAME = value  Example : final double CENTIMETERS_PER_INCH = 2.54; final int NO_OF_STUDENTS = 20; final char BLANK = ' '; final double PAY_RATE = 15.75;
  • 9. Data Types  A set of values together with a set of operations.
  • 10. Primitive Data Types • The primitive data type are the fundamental data types in Java.
  • 11. Primitive Data Types • Integral data types (deals with integers, or numbers without a decimal part):
  • 12. Primitive Data Types  Floating-point data types (deals with decimal numbers):  float  double  boolean (deals with logical values):  True  False
  • 13. Values and Memory Allocation for Integral and Floating-point Data Types
  • 14. Characters  A char variable stores a single character  Character literals are delimited by single quotes: 'a' 'X' '7' '$' ',' 'n'  Example declarations: char topGrade = 'A'; char terminator = ';', separator = ' ';  Note the difference between a primitive character variable, which holds only one character, and a String object, which can hold multiple characters
  • 15. Characters  A character set is an ordered list of characters, with each character corresponding to a unique number  A char variable in Java can store any character from the Unicode character set  The Unicode character set uses sixteen bits per character, allowing for 65,536 unique characters  It is an international character set, containing symbols and characters from many world languages
  • 16. Characters  The ASCII character set is older and smaller than Unicode, but is still quite popular  The ASCII characters are a subset of the Unicode character set, including: uppercase letters lowercase letters punctuation digits special symbols control characters A, B, C, … a, b, c, … period, semi-colon, … 0, 1, 2, … &, |, , … carriage return, tab, ...
  • 17. Boolean  A boolean value represents a true or false condition  The reserved words true and false are the only valid values for a boolean type boolean done = false;  A boolean variable can also be used to represent any two states, such as a light bulb being on or off