We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 7
Variable Declaration
A variable is a value that can be change depending on conditions or on
information passed to the program, for example, we have a text string “Welcome
Students!!!” or a variable score with a data type integer with a value 10. A variable
can then be reused through out your program, instead of having to type out the
actual value all over and over again.
In Java programming, you can define a variable with the following form:
Data Type Variable Name and Value let say:
String User_Name ~ “Jake R. Pomperada”
The data type is String which refers to a series of characters our variable name
is Username and our value is “Jake R. Pomperada”. Always remember that Java
is case-sensitive programming language similar to C/C# and C++.
Rules in Using Variables
A variable can contain:
1. Any unicode character that is a letter (including numeric letters like Roman
numerals) or a digit.
2. Currency sign (such as $),
Connecting punctuation character (such as _).
A variable cannot:
1. Variable names are case-sensitive.
2. Avariable’s name can be any legal identifier.
3
It can contain unicode letter, digits and two special characters such as
underscore and § dollar sign.
Length of Variable name can be any number.
It is necessary to use alphabet at the start (however, we can use underscore).
Some auto generated variables may contain ‘S' sign. But try to avoid using
dollar sign
White space is not permitted.
awe.
=u
Special Characters are not allowed.
9. A digit at the start is not allowed.
10. Subsequent characters may be letters, digits, dollar signs, or underscore
characters.
11, Variable name must not be a keyword or reserved word.Constant Variables
Constant variables refers to those variables whose value does not change
during program execution. This type of variables is very useful if we want to
assign value to a variable that is fixed. For example final tax = 26.7; this example
we declare a variable tax with a value of 26.7 this variable becomes constant by
declaring the keyword final before the name of our variable tax as we assigned a
value of 26.7.
Example:
import java.text. DecimalFormat;
public class constant {
// Method for rounding off number and setting decimal places
public static float round(double value, int places) |
float p = (float)Math.pow(10,places);Data Type
This is a set of data that specifies the possible
the operations that can be performed on the values,
values are stored in memory. Understanding this data ty}
to manipulate it appropriately
range of values of the set
and the way in which the
pe enables the computer
Primitive Types
Type Name | Wrapper Value Range
class
byte java.lang. integer =128 through +127
Byte
short java.lang, | integer —32,768 through +32,767
Short.
int java.lang. | integer =2,147,483,648 through
Integer +2,147,483,647
long java.lang. | integer ~9,223,372,036 854,775,808 through
Long +9,223,372,036,854,775,807
float javalang. | floating #1.401298E-45 through
Float point 43.402823E+38
number
double java.lang. | floating +#4.94065645841246E-324 through
Double point
number
boolean javalang. [Boolean | true or false
Boolean
char javalang. | UTF-16 code |’\u0000" through ’\uFFEF”
Character__[unit
=Java Operators
Java provides a rich set of operators enviroment. Java operators can be divided
into the following, categories:
Arithmetic operators
Relational operators,
Logical operators
Bitwise operators
Assignment operators
6. Conditional operators
Arithmetic operators
Arithmetic operators are used in mathematical expression in the same way
that are used in algebra.
OPERATOR DESCRIPTION
+ adds two operands
- subtract second operands from first
_ multiply two operand
7 divide numerator by denumerator
% remainder of division
+ Increment operator increases integer
value by one
a Decrement operator decreases integer
value by one
Relational Operators
The following table shows all relational operators supported by Java.
OPERATOR DESCRIPTION
= Check if two operand are equal
= ‘Check if two operand are not equal.
> Cheek if operand on the left is greater
than operand on the right< ‘Check operand on the left is smaller
than right operand
> check left operand is greater than or
equal to right operand
< Check if operand on left is smaller than
L or equal to right operand
Logical Operators
Java supports three logical operators. Suppose a=1 and
Operator__| Description [Example
&& Logical AND |(a &é& b) is
false
i Logical OR [(a 11 b)is true
! Logical NOT | (la) is false
Bitwise Operators
Java defines several bitwise operators that can be applied to the integer types
long, int, short, char and byte.
Operator Description
& Bitwise AND
D Bitwise OR
s Bitwise exclusive OR
<< left shift
>> [right shift
Now lets see truth table for bitwise &, | and *
a b a&blalb [a*b
0 0 0 0 0
o 1 0 1 i
1 0 0 1 1
1 i [i 1 0e left oper ecifies the
The bitwise shift operator shifts the bit value. The left operand spec ¢
value tobe shifted and the right operand specifies the number of positions that the
bits in the value are to be shiited. Both operands have the same precedence.
Example:
a= 0001000
be2
a<> b= 0000010
Assignment Operators
Assignment operators supported by Java are as follows
Operator Description Example
assigns values from right side operands fo
= Jieft side operand a
a= |@dds right operand to the left operand and
assign the result to left
subtracts right operand from the left operand
at=b is same as a=atb
*__|and assign the result to left operand ee
+= | Mutiply left operand with the right operand | :
= | and assign the result to left operand Ae nee ae
divides left operand with the right operand
1 J and assign the result to left operand afb is same as a~a/b
oye [calculate modulus using two operands and) a%e-b is same as
o* __ [assign the result to left operand a=a%b
Escape Characters
Escape characters (also called escape sequences or escape codes) in general
are used to signal an alternative interpretation of a series of characters. In Java,
a character preceded by a backslash (\) is an escape sequence and has special
‘meaning to the java compiler.
Unicode character [\u3876 (\u followed by the hexadecimal
unicode code point up to U+FFEF)
‘Octal escape \352 (octal number not exceeding 377, preceded
by backslash)
Line feed \nCarriage return
Form feed
Backslash
Single quote
Double quote
Tab [At
Backspace [\y