Syntax, Data Type, Variables, Array, Casting
Data type
the type of value that a variable can accommodate is known as data type. When user declare a variable it is required to specify:
a) The type of value it will hold.
b) variable name.
This assures the compiler that the particular variable will hold certain amount of memory to store values. For instance, num is int type and takes two bytes to accommodate the integer value, bol is a boolean type and takes one bit to accommodate a boolean value .
Some generally used types of data types are utilized in the programming languages known as the primitive types like characters, integers, floating point numbers etc. These primitive data types are shown below where size denotes the memory size it takes.
For example, boolean takes a value "true"/"false" in 1 bit memory. It takes value "false" if not initialized (in the case of non-local variables)
Java Primitive Data Types
Data Type
|
Description
|
Size
|
Default Value
|
boolean
|
true or false
|
1-bit
|
false
|
char
|
Unicode Character
|
16-bit
|
\u0000
|
byte
|
Signed Integer
|
8-bit
|
(byte) 0
|
short
|
Signed Integer
|
16-bit
|
(short) 0
|
int
|
Signed Integer
|
32-bit
|
0
|
long
|
Signed Integer
|
64-bit
|
0L
|
float
|
Real number
|
32-bit
|
0.0f
|
double
|
Real number
|
64-bit
|
0.0d
|
Variables in Java
A variable denotes to the memory location that holds values as if numbers, texts etc. are in the computer memory used. A variable is a identify of location where the data is stored when a program executes.
In Java there are the following types of variables:
1. Instance Variables also known as Non-static fields: In object oriented programming, objects are store in their individual states in the non-static fields that is declared without the static keyword. Each object of the class has its possess set of values for these non-static variables, thus these are related to objects. Therefore these variables are also known as instance variables. These variables consider default values if not initialized.
2. Class Variables or Static fields: These are jointly related to a class and none of the object can lay claim for them. The variables determined with static keyword are shared by all objects. In this case, objects do not store an individual value but they are constrained to share it among themselves. These variables are stated as s" using the static keyword. The same set of values are apportioned in between distinct objects of the same class. So these variables act as global variables which are same for all objects of the class. These variables consider default values if not initialized.
3. Local Variables: The variables specified in a method or block of code is referred as local variables. These variables can be get in the method or block of code only. These variables never take default values if not initialized. They are asked to be initialized before using them.
Casting
In java, typecasting one object reference can be type cast into another object reference. The frame can be of its own class type or to its subclass/superclass types. For casting in java, there are certain compile run time rules and time rules.
Any object reference can be attributed to a reference variable of the Object type.For every Java class,the Object class is a super class. There are two casting java assumptions:
i) Upcasting
When we cast a reference along the class hierarchy in a direction from the sub classes towards the root,the process is referred as Upcasting.
ii) Downcasting
When user cast a reference along the class hierarchy in a direction from the root class towards subclasses, the process is referred as Downcasting.
Selection Statements (If, Switch)
The aim of switch statement to select one of many possible statements to execute. If statement permits user to select one of two sections of code to execute based on a boolean value (i.e. two possible values). The switch statement allows you to select from many statements based on an integer (including char) or enum value.
The Syntax is as following:
switch (expr) {
case c1:
statements // do these if expr == c1
break;
case c2:
statements // do these if expr == c2
break;
case c2:
case c3:
case c4: // Cases can simply fall thru.
statements // do these if expr == any of c's
break;
. . .
default:
statements // do these if expr != any above
}
Control Statement (while, do while, for)
The while Statements
The while statement sSeemingly without interruption executes a block of statements while a specific condition is true. Its syntax can be expressed as:
while (expression) {
statement(s)
}
The while statement measures expression, which must return a boolean value. If the expression assesses to be true, the while statement executes the statement. The while statement continues test the expression and executing its block until the expression assesses to be false.
The do-while statement
The Java also supports a do-while statement, which can be expressed as follows:
do {
statement(s)
} while (expression);
The main difference between do-while and while is that do while assesses its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once.
The for Loop
A for loop is a repetition control structure that permits user to efficiently write a loop that needs to execute a specific number of times.
The syntax is as following:
The syntax of a for loop is:
for(initialization; Boolean_expression; update){
//Statements
}
Arrays
An array is a common type of data structure where in all elements must be of the similar data type. Once it is defined, the size of an array is fixed and cannot increase to accommodate more elements. The first element of an array starts with zero.
The example is as following:
x0=0
x1=1
x2=2
x3=3
x4=4
x5=5
where,
x[0]=0
x[1]=1
x[2]=2
x[3]=3
x[4]=4
x[5]=5
The index can be referenced by a variable for easy looping.
for(count=0; count<5; count++) {
System.out.println(x[count]);
}
Using and array in the program is a 3 step process that involves:
1) Declare a set of array
2) Construct array
3) Initialize the array
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your java homework and assignments? Live java experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer java homework help, java assignment help and Java projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
ExpertsMind.com - Fundamentals of Java Assignment Help, Fundamentals of Java Homework Help, Fundamentals of Java Assignment Tutors, Fundamentals of Java Solutions, Fundamentals of Java Answers, Java Programming Assignment Tutors