Home | | Object Oriented Programming | Java - Data Types, Variables

Chapter: Object Oriented Programming(OOP) : Overview of Java

Java - Data Types, Variables

Data Types: • For all data, assign a name (identifier) and a data type

DATA TYPES, VARIABLES

 

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

 

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

         Names of variables should be meaningful and reflect the data they will store

–   This makes the logic of the program clearer

         Don't skimp on characters, but avoid extremely long names

         Avoid names similar to Java keywords

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; 

long cityPopulation;     

 

Floating-Point Data Types

         Numbers with fractional parts

 

Example declarations: float salesTax; double interestRate;

 

double paycheck, sumSalaries; char Data Type

 

         One Unicode character (16 bits - 2 bytes)


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, …;

 

Literals

         int, short, byte

Optional initial sign (+ or -) followed by digits 0 – 9 in any combination.

         long

 

Optional initial sign (+ or -) followed by digits 0–9 in any combination, terminated with an L or l.

 

***Use the capital L because the lowercase l can be confused with the number 1. Floating-Point Literals

 

         float

 

Optional initial sign (+ or -) followed by a floating-point number in fixed or scientific format, terminated by an F or f.

 

         double

 

Optional initial sign (+ or -) followed by a floating-point number in fixed or scientific format. 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.

 

Compatible Data Types

Any type in right column can be assigned to type in left column:

 

Data Type     Compatible Data Types

byte                byte

short               byte, short

int                   byte, short, int, char

long                byte, short, int, long, char

float                float, byte, short, int, long, char

double            float, double, byte, short, int, long, char

boolean          boolean

char                char

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Object Oriented Programming(OOP) : Overview of Java : Java - Data Types, Variables |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.