Java Language Basics
· Variables
Previously,we learnt that object store their state in fields.Java programming language has variables that are used to store values.Lessons to learn in this section.
How to name variables.
Types of data types.
· Operators
This section describes some of the methods used in java.
· Expressions,statements,blocks
Generally,operators are used to create expressions with can compute values.Statements contain expressions.Several statements grouped together will make up blocks.
· Control Flow statements
Control flow statements enables program to conditionally execute certain code blocks and leave others.
1. Variables
In previous lesson (What is an Object )you learnt that object store it state in fields. Eg int gearNumber,int speed etc.Field and Variables are commonly used.they Just mean the same thing.In this lesson you will learn
I. How to name variables.
II. How to declare variables
III. Different types of Variables
Variable types
Instance variables(non-static fields)-non-static methods are also know as instance variables.These variables are unique to each class.
Class Variables-These are variables that are defined with static keyword.Eg static int gearNumber=0.Static variables no matter how many times the class is instaniated,the copy remains. Note:
Only static methods can change value of a static variable.
If a static variable is declare using final keyword,not even static method can change value of the variable
Local variables-These are variables used by methods to store their state which is temporary.Declaring local variables is simple :- int gearNumber=0;
Parameters –These are variables that are passed to method.Class main method take array of arguments as parameters. Example
//Static main method
public static void main(String [] args)
{
}
Rules for naming Variables in Java.
All programming languages have rules that guide programmers in naming variables.
Ø Variables names are case-sensitive,All variables should begin with a letter,$ sign or _ underscore.
Ø Do not use Java reserved keywords when naming variables.
Ø Using full words when naming variables like int gear instead of abbreviations like g makes the code easy to read.
Ø If a variable has one word,use all small letters eg int gear=0 and incase it has two words,capitalize the first letter of the second word i.e int gearNumbers=0.And incase the variable is a constant,capitalize all the words and separate them with underscore Eg static final int GEAR_NUMBERS=2.
Primitive Data Types
All Java variables before using them,you must declare their type and give them a name.eg int gearNumbers=0.
Variable’s data type determine the value it can hold and operations that can be performed on the variable.
Primitive data types are predefined in every programming language.Java supports eight types of primitive data types.These are
Ø Byte – is a 8-bit signed.Its has a minimum of -128 and maximum of 127.Bytes can be used in saving memory especially in arrays.
Ø Short- is a 16-bit signed.Minimum=-32,768 and a maximum =32,767.
Ø Int –is a 32 bit signed. minimum value of -231 and a maximum value of 231-1.
Ø Long-is a 64-bit two's complement integer. minimum value=-263 and a maximum value =263-1.
Ø float: The
float
data type is a single-precision 32-bit IEEE 754 floating point.
Ø double: The
double
data type is a double-precision 64-bit IEEE 754 floating point.
Ø boolean: The
boolean
data type has only two possible values: true
and false
.
Ø char: The
char
data type is a single 16-bit Unicode character.
To additional 8 data types Java language introduces String variables that can be declared and assigned a value using double quotes.Eg String websiteName=”techoverload.net”.String variables once created they are immutable,that means they cannot be changed.
Default Values
When declaring variables its not necessary to assign a field. If not initialized there values are set to default null or zero.
Data Type
|
Default Value (for fields)
|
byte
|
0
|
short
|
0
|
int
|
0
|
long
|
0L
|
float
|
0.0f
|
double
|
0.0d
|
char
|
'\u0000'
|
String (or any object)
|
null
|
boolean
|
false
|
Local variables are never assigned default value. All local variables are initialized when they are declare. Accessing uninitialized variable will result to error.
Literals
New keyword is never used when initializing a primitive type of variable. Literal is a source code representation of a fixed value.
boolean result = true;
char capitalC = 'C';
byte b = 100;
short s = 10000;
int i = 100000;