Está en la página 1de 8

Java Language Basics

Expectations:
The student will:
1. be able to define the term: variable
2. explain the primitive variable types
3. declare a primitive variable
4. assign a value to a primitive variable
5. know the various comparison operators
6. know the various types of math operations
7. know the shortcut assignment operators
8. know how to define constants
9. explain casting
10. be familiar with methods of the Math class
11. know the general pattern for coding a class

A variable is a named storage location in RAM


JAVA has the following primate variable types:
Numbers without Decimal Portion:

Java Variable Type Name Size


byte

1 byte

short

2 bytes

int

4 bytes

long

8 bytes

Numbers with Decimal Portion:

Java Variable Type Name Size


float

4 bytes

double

8 bytes

One Alphanumeric Character

Java Variable Type Name Size


char

2 bytes (2 bytes allow for international symbol set)

True or false flag

Java Variable Type Name Values


boolean

true and false only

Declaring Variables

creates storage place in RAM


must be done before a variable can be used
can be done just before variable is used (no need to put all declares at top of
methods)
Java is case sensitive, types must be all in lowercase

General Format:
Java-Variable-Type-Name Variable-Name Semi Colon
Variable Naming Convention

primitive variable names start with a lower case letter


name may contain letters, numbers and underscore
all letters are lower case, except where a name is composed of multiple words,
then the subsequent words have their first letter capitalized

Examples :
boolean happy;
char gender;
double robsPay;
Assignment Statement

Assigns a value to a variable.


Java uses =

Examples:
int age;
age = 16;
long age;
age = 197L; (L indicates a long value)
double robsPay;
robsPay = 13.50;
float robsPay;
robsPay = 13.50f; (f indicates a float value)
char gender;
gender = m; (Note: single quotes around the character value)
boolean happy;
happy = false;
Primitive Variable Type Comparative Operators

Operator

Symbol Example

equals

==

if (x = = y)

not equals

!=

if (x != y)

less than

<

if (x < y)

greater than

>

if (x > y)

less than or equal

<=

if (x <= y)

greater than or equal >=

if (x >= y)

Math Operations
Java follows BEDMAS and left-to-right evaluation when two or more operations of same
precedent occur without brackets

Operation

Symbol Example

addition

a = b + 2;

subtraction

a = b - 2;

multiplication

a = b * 2;

division

a = b / 2;
Note: if b is integer, then the answer has all decimals
truncated, even if a is a float or double

integer division
remainder (mod)

a = b % 2;

++

a++;
adds 1 to a, after operations involving a are completed eg.
if (a++ < 2) -> a is incremented after the comparison is
performed

pre increment

++

++a;
adds 1 to a, before operations involving a are completed
eg. if (++a < 2) -> a is incremented before the comparison
is performed

post decrement

--

a--;
same comment as a++

pre decrement

--

--a;
same comment as ++a

post increment

Note: Java does division according to the operand types:


double answer;
answer = 7/2;
answer is 3 not 3.5 !!
because 7 and 2 are integers
answer = 7.0 / 2;

answer is 3.5
because 7.0 is a double
ShortCut Assignment Operators

Operator

Symbol Example

assign addition

+=

a += 2;
means: a = a + 2;

assign subtraction

-=

a -= 2;
means: a = a - 2;

assign multiplication *=

a *= 2;
means: a = a * 2;

assign division

/=

a /= 2;
means: a = a / 2;

assign mod

%=

a %= 2;
means: a = a % 2;

Other math functions are built into the Math Class


See the Math Class documentation for : cos, sin, tan, absolute value, PI, max, min,
random number, square root
To raise x to the power of 5 :
double x = 3;
double answer;
answer = Math.pow(x,5);

Constants in Java

A constant is similar to a variable, except its value may not be changed.


The Java keyword final is used to create a constant.
They are used to make your code easier to understand.
They are used to make your code easier to change (it is easier to change the value
for GST in one place than many)
No numbers of any kind are to appear in code - always use constants

Example
private final double GST = 0.07;
private final double MINIMUM_WAGE=6.75;
Coding Standards - Constant names have all letters capitalized

Converting Between Variable Types:


Safe
It is always safe to convert from a variable type that can hold less information to a type
that can hold more information, so the following conversions are safe to perform:
byte -> short -> int -> long -> float -> double
Example:
int age;
long bigAge;
age = 17;
bigAge = age;
Unsafe
When converting from a variable type that can hold more information to a variable type
that can hold less information, you may lose information (for example, decimals may be
truncated), so this is unsafe conversion and you must explicitly tell Java you desire to
perform the conversion.
Casting is the technique used when performing an unsafe conversion.
Casting syntax is: (Convert-to-Variable-Type)
Example:
double age = 17;
int newAge;
newAge = age; WRONG (type cast error)
newAge = (int)age; CORRECT Casting

Java Class Pattern


public class MyClass
{
public int getMyVariable1()***
{
return myVariable1;
}
public void setMyVariable1(int newMyVariable1)**
{
myVariable1 = newMyVariable1;
}
private int myVariable1; *
private int myVariable2; *
}

Notes:
* 2 variables declared

the attributes of the class (often called instance variables)


can be used in any of the class's methods (essentially global variables inside the
class)
they are private so only methods inside the class can see and change them, they
are invisible to any code outside this class (such as another class )

** mutator method

used to change the value of an instance variable of the class


name of method starts with set is public so other classes can run this method
void means nothing is returned from this type of method
(int newMyVariable1) is the parameter type and local name of the parameter
which is sent in from the calling class

*** accessor method

used by other classes to get the value of an instance variable


name of method starts with get is public so other classes can run this method
int is type of variable returned by this method
() no parameters sent in

Public / Private General Rules


All attributes (instance variables) are private

All methods are public

Unless it is a helper methods that other public methods will use, but never need
to be used by outside classes, in which case it is private
Example : A simple calculation used by a number of other methods

Order of Class Contents


1. all public methods, in alpha order by method name
2. all private methods, in alpha order by method name
3. all instance variables

También podría gustarte