Java - DateTypes - Variables - Operators

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Datatypes, Variables

& Operators
DataTypes
 Data types specify the different sizes and values that
can be stored in the variable.
There are two types of data types in Java:
 Primitive data types: The primitive data types
include boolean, char, byte, short, int, long, float and
double.

 Non-primitive data types: The non-primitive data


types include Classes, Interfaces, and Arrays.
Data Types, Variables, and
Constants
 Declaring Variables
 Primitive Data Types
 Initial Values and Literals
 String Literals and Escape Sequences
 Constants
Declaring Variables
 Variables hold one value at a time, but that value
can change
 Syntax:
dataType identifier;
or
dataType identifier1, identifier2, …;
 Naming convention for variable names:
 first letter is lowercase
 embedded words begin with uppercase letter
Declaring Variables
 Names of variables should be meaningful and reflect
the data they will store
 This makes the logic of the program clearer
 Avoid names similar to Java keywords
Scope of a Variable
 In programming, a variable can be declared and
defined inside a class, method, or block. It defines the
scope of the variable i.e. the visibility or accessibility of
a variable.
 Variable declared inside a block or method are not
visible to outside.
 We can declare variables anywhere in the program but
it has limited scope.
Scope of a Variable
 A variable can be a parameter of a method or
constructor.

 A variable can be defined and declared inside the body


of a method and constructor.

 It can also be defined inside blocks and loops.

 Variable declared inside main() function cannot be


accessed outside the main() function
Scope & lifetime of variable
DataTypes
Data Type Size
boolean 1 bit
char 2 byte
byte 1 byte
short 2 byte
int 4 byte
long 8 byte
float 4 byte
double 8 byte
Data Types
 For all data, assign a name (identifier) and a data
type
 Data type tells compiler:
 How much memory to allocate
 Format in which to store data
 Types of operations you will perform on data
 Compiler monitors use of data
 Java is a "strongly typed" language
 Java "primitive data types"
byte, short, int, long, float, double, char, boolean
Integer Types - Whole Numbers
Type Size Minimum Value Maximum Value
in Bytes
byte 1 -128 127
short 2 -32,768 32,767
int 4 -2, 147, 483, 648 2, 147, 483, 647
long 8 -9,223,372,036,854,775,808 9,223,372,036,854,775,807

Example declarations:
int testGrade;
int numPlayers, highScore, diceRoll;
short xCoordinate, yCoordinate;
byte ageInYears;
Floating-Point Data Types
 Numbers with fractional parts
Type Size Minimum Value Maximum Value
in Bytes
float 4 1.4E-45 3.4028235E38
double 8 4.9E-324 1.7976931348623157E308

Example declarations:
float salesTax;
double interestRate;
double paycheck, sumSalaries;
char Data Type
 One Unicode character (16 bits - 2 bytes)
Type Size Minimum Value Maximum Value
in Bytes
char 2 character character
encoded as 0 encoded as FFFF

Example declarations:
char finalGrade;
char newline, tab, doubleQuotes;
boolean Data Type
 Two values only:
true
false
 Used for decision making or as "flag" variables
 Example declarations:
boolean isEmpty;
boolean passed, failed;
Assigning Values to Variables
 Assignment operator =
 Value on the right of the operator is assigned to the
variable on the left
 Value on the right can be a literal (text representing a
specific value), another variable, or an expression
(explained later)
 Syntax:
dataType variableName = initialValue;
Or
dataType variable1 = initialValue1,
variable2 = initialValue2, …;
Assigning the Values of Other
Variables
 Syntax:
dataType variable2 = variable1;
 Rules:
1. variable1 needs to be defined before this statement
appears in the source code
2. variable1 and variable2 need to be compatible
data types; in other words, the precision of
variable1 must be lower than or equal to that of
variable2.
Sample Assignments
 This is a valid assignment:
float salesTax = .05f;
double taxRate = salesTax;

 This is invalid because the float data type is lower in


precision than the double data type:
double taxRate = .05;
float salesTax = taxRate;
String Literals
 String is actually a class, not a basic data type; String
variables are objects
 String literal: text contained within double quotes.
 Example of String literals:
"Hello"
"Hello world"
"The value of x is "
String Concatenation Operator (+)
 Combines String literals with other data types for
printing

 Example:
String hello = "Hello";
String there = "there";
String greeting = hello + ' ' + there;
System.out.println( greeting );
Output is:
Hello there
Operators in Java
Category What it does… Examples

Unary Increment, Decrement ++, --


Arithmetic Addition, subtraction +, -, /
Assignment Set a value to an expression =, +=, &=

Conditional Choose one of two values ?:

Logical Logical comparisons &&, ||


Relational Compare values ==, >=
Bitwise Move bits within a number <<, >>

You might also like