|
|
|
|
|
|
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
Variables
Rules for creating variables
Data Types: A programmer, while programming has to deal with various types of data. Hence, it becomes necessary that the programming language facilitates the programmer with all data types. Java provides all these.
Integers: These are whole numbers suck as 2, 4, 6, -7, -9 etc. no fractional part is an integer. Integers are divided into four categories.
Example of Valid Integers int no1; int no1=0 int sum; int sum = 0 int rollno; int rollno = 0 Initialization of Variables int no1 = 5 int rollno = 34
Character Types Character types are 2 bytes in size but can hold single characters. A character constant varies from 0 to 255. The characters are stored in the computer memory as integers in their binary form. Each character is assigned a particular value according to ASCII codes.
Example of a Valid Character char reply = y char ans = a
String is a series of characters. These characters may be alphabets, digits, blank spaces etc. the string is enclosed within double quotes. The string is used to write or store messages.
Example: “Hello” “Hello” is a string and the characters of a string are stored in sequential locations. String Variables: String name; String vh1; String Address;
Floating point: Float data type: a fractional number is a floating point number. Decimal in a number refers that it is a floating number. Floating points are divided into two parts:
Example:
float average; float balance; double gross salary; double percentage;
Constants: Constants are fixed values that do not change during the execution of the program. Constants are also called literals. There are numeric and character constants. Numeric constants are divided into Integer constants float. Character constants are divided into character and string.
Operators
Arithmetical Operators: These are used for mathematical calculations. The result of an arithmetical expression is a numerical value. An arithmetical expression may be a variable or a constant or a combination of both. These variables or functions are connected with arithmetical operators.
Relational Operators These are used to determine the relationship between different operands. These are used in the work of comparison also; these are two mathematical expressions and are connected by relational operators. The relational expression returns 0 if the relation is false and returns 1 if the relation is true. The six relational operators are shown in the following table:
Logical Operator Logical operators combine the result of two or more than two expressions. They are made of connecting relationships in the expression refers as logic and the expressions are called logical expressions. The logical operator returns 0 if the relation is false and 1 if its true.
Increment & Decrement Operators Java provided ++ and – which are not provided my many other languages. These operators are called increment and decrement operators. The increment operator adds 1 to the operand and decrement operator subtracts 1 from the operand.
Let the operand variable be i ++i / i++ means i=i+1 --i / i-- means i=i-1
The operators exist in two types
Example: Prefix increment operator: consider the following program segment
class sample { public void prefixprog() { int i=20; int y=++i*8; System.out.println ("Answer is: "+y); } }
Output: Answer is: 168
Postfix increment operator: consider the following program segment
class sample { public void postfixprog() { int i=20; int y=i++*8; System.out.println ("Answer is: "+y); } }
Output: Answer is: 160
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||