Why Java
Java Basics, Data Types, and Arithmetic
Variables and Computer Memory
Variables are used to store values. The operating system reserves locations in memory for each variable declared in a program. The value of a variable can change during program execution.
Constants
The value of a constant remains invariable during program execution. Constants are declared with the keyword final. By stylistic convention, constant names are all uppercase.
Literal Constant
final int WIDTH=20; //20 is a literal constant
myLbl.setText(“Hello World”); //Hello World is a literal constant
Symbolic Constant
Name of constant is used
m = WIDTH + 10; //WIDTH is a symbolic constant
Data types (by precision with maximum)
Integer types
byte 127
short 32767
int 2.1 billion (int is the default integer type)
long 9 x 1018
real types (decimal)
float 3.4E+38
double 1.8E+308 (double is the default real type)
char
a character in single quotes (‘)
char b = ‘$’;
Scientific Notation (double)
4.7E03 means 4.7 x 103
String (String Object)
Text enclosed in double quotes (“)
String name = “Sally”;
Variable declaration
int x;
int i,j,k;
Variable declare and initialize
int i=0;
double y = 8;
double x= 3*y;
Variable Assignment (= operator)
x = 21;
y = 4.7;
z = x + y; //x + y is evaluated and the result is stored in z
Arithmetic Operators
Add +
Subtract -
Multiplication *
Division /
Modulus % ( also called Remainder – usually used with integers)
When division is used with 2 integers the result is int. If one value is double, the result is double. For example if a = 12 and b = 5, x = 12.0 and y = 5.0:
a/ b is evaluated as 2
a / y is evaluated as 2.4
x / b is evaluated as 2.4
Arithmetic Operators - Rules of Precedence
(recall the phrase “Please Excuse My Dear Aunt Sally.”)
Sub expression ( )
Unary + or -
Multiplication/Division * / % (multiple * / % evaluate left to right)
Addition/Subtraction + - (multiple + - evaluate left to right)
Math Class
The Math class is built-in – no import statement is needed. Dot syntax is used for Math methods. The Math.pow method is used for exponentiation.
double x = Math.min(a,b); //minimum of a and b
area = Math.PI*r*r; //area of a circle with radius r
y = Math.pow(5.6,3),x,y); //the cube of 5.6
r = Math.random(); //random number in the range 0 - 1
z = Math.sqrt(405.33) //square root
Casting
Implicit Assignment Conversion uses Numerical Promotion
double x = 5; //stores 5.0 in x
Explicit - type cast operator
(double) x/3
Use casting to change the result of integer division to double. If a is 12 and b is 5:
(double) a / b is evaluated as 2.4. Without the cast, the result is 2.
Assignment Statement
sum = sum + x; // The new value of sum (the left had side or LHS) is the old value of sum (right hand side or RHS) plus x. The LHS of an assignment statement can list only one variable name. The RHS contains a variable name, value, or expression. The expression is evaluated and the result is stored in the variable (implicitly cast to the correct type).
Accumulator Expressions
Example: sum += X;
Operators are += -= /= *= %=
Increment Operators
Example: y++;
Operators are ++ --
Formating
Include the following import statement: import java.text.*;
For double (Inserts commas when needed)
NumberFromat myFormat = NumberFormat.getInstance();
myFormat.setMaximumFractionDigits(3);
myFormat.setMinimumFractionDigits(1);
String numStr = myFormat.format(num);
For Currency (Includes dollar sign and 2 decimal places)
NumberFromat myCurFormat = NumberFormat.getCurrencyInstance();
String currStr = myCurFormat.format(num);
For percents
NumberFormat pctFormat = NumberFormat.getPercentInstance();
String pctStr = pctFormat.format(num);
Exceptions (catching errors in numeric input)
try
{
//get data
int num = Integer.parseInt(ageTxt.getText());
}
catch(NumberFormatException err)
{
//handle the exception
showStatus(“Bad data”);
}
Wrapper Class Conversion Methods
String to Numeric
double a = Double.parseDouble(aTxt.getText());
int j = Integer.parseInt(jTxt.getText());
Numeric to String
String nStr =”” + num;
(There are also valueOf functions from Java 1 that are now deprecated).
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.