Académique Documents
Professionnel Documents
Culture Documents
An object-oriented language.
A compiled language.
It translates (compiles) human-readable source code to machine or binary code.
Using javac, source code (.java) is compiled into binary (.class).
All .class files are cross-platform.
Different hardware and operating systems can use Java.
Thus, different versions of Java are not necessary.
(Normally, an application rests above its operating system, which is above its hardware.)
Java, however, rests upon a jvm (java virtual machine) which is unique for the operating
system being used.]
javac compiles, and java runs.
Miscellanea:
Java can be memory intensive, because of its nature.
Some functions are imported (such as java.io.* and java.util.*), using the import command.
Every statement ends with ;.
Every statement not having a { or } ends in ;.
Commenting out text:
/* Many
words. */
// single line.
Javac ignores comments.
As long as .class files are in the same directory, they can reference one another's classes and
methods.
Objects
Java is object oriented, in that it is a collection of objects.
Objects have 2 parts:
Instance variables are what objects have and know.
Methods are what objects can do.
Methods are followed by parentheses (method( )).
A class is a blueprint for all objects of the same type.
An instance is a specific individual of a given class.
If objects are in the same class, methods will be the same, but instance variables will not be.
A filename always has to be the same as the class it describes (always capital).
Classes list all the instance variables, and then all the methods.
Primitive Data Types
Every java variable must have a specific type.
Data types are either classes or primitives.
Primitive data types are not objects.
They are used to represent basic value types, like letters and numbers.
Mathematical operators are not methods.
Type
Name
Size (bits)
Range
integers
byte
-128 127
short
16
-215 215-1
32
-231 -231-1
( 2 billion)
long
64
-263 263-1
( 1018)
float
32
7 digits
64
14 digits
boolean
true, false
characters
char
16
(letters, digits, symbols)
Calculating range:
Minimum is (2(number of bits 1)).
Maximum is 2(number of bits 1) 1.
A bit is a single 1 or 0.
A byte is composed of 8 bits.
Java Programs
Java programs have reserved, key words.
These are terms that are part of the Java language.
They are not external.
For example: public, import, variable types.
Variables are used to refer to a piece of memory with a value.
Every variable must have a type.
When you type <type> <variable>, this is known as declaring the variable.
A variable always refers to the same location in memory, and thus has the same value.
Names:
No spaces.
Reserved words cannot be used as variables.
Letters and numbers can be used.
Underscores and caps can be used.
Camel case is used in names new words have capitals but no spaces, likeThis.
Class names also follow conventions.
They are usually in upper case.
They can be used as a types.
Method names, however, are lower-cased.
Literals are denoted by ' on both sides.
They are obvious constants.
They include numbers.
A variable, like pi, can be fixed.
Single characters (when in single quotes).
For example: 'a', 'R', 't', '3'.
If you add literals, you will get their corresponding values.
'a' + 3 adds 3 to the numerical equivalent of a.
a + 3 simply a variable.
'3' + 1 adds the values, BUT displays as a literal, thus you will get '4'.
Special literal characters:
These must be in quotes.
'\n' new line.
'\t' tab.
'\b' beep/
Variables
A variable must first be declared.
They can only be declared once, as variables cannot be reused as other data types.
Primitive variables must be given a value before being used, which is known as initialization.
For example, int x = 10 sets the value of x to 10.
Objects must be instantiated, or an instance of it must be created.
A constructor is used for this purpose.
new is the keyword used to construct.
A variable is used to identify memory.
It is initially null.
After instantiation, it refers to a particular piece of memory.
An object is necessary to call a method.
. is used, meaning member-of operation.
z.teach( ), for example. Teacher z uses the teach method here.
String is a preset Java class.
A string is a group of 1 or more characters.
Even though it is not a primitive, a string can be initialized.
Strings can be combined with +.
For example, a = b + hi. Or, a = my + pin will put the number into the string.
Methods to print strings:
System.out.print( ).
System.out.println( ).
When an object is treated as a string, Java will look for a toString( ) method.
By default, treating an object as a String results in <name of class> @ <memory
address>.
The scope for a parameter variable is in the method itself.
It will cease to exist outside that variable.
This is different from instance variables.
Dividing an int by an int, despite resulting in a double result, will be displayed as an int.
Java will go to a more precise type. (double + int = double).
Type casting tells Java to treat a value as different from its native type.
Though Java always upgrades to a higher precision type, and yields an error if one would
get a less precise type, a less precise type can be yielded.
Type casting works with primitive numeric types.
Objects primitives cannot be typecasted.
Failed example: double x; int y; y = x; (an int cannot be set to a double value because this
will result in a lowering of precision).
This will result in an error message upon compiling.
Correct example: y = (int)x;.
This views the double x as an int, and allows a lowering of precision.
Random Math.random( ) returns a double in the range [0, 1).
If double g = Math.random( ); int x = (int) (Math.random( ) * 4), this will randomly result in
the integers 0, 1, 2, or 3.
Default values for non-primitive values are assigned by the default constructor.
Numbers 0 or 0.0.
Characters null character (' '), which can also be represented as '\0', which has the value
of 0, and is not the digit, which actually has a higher value.
Booleans false (0 = false).
Objects null, will not be assigned memory until instantiated.
Conditional Statements, Booleans, & Comparison Operators
Conditional statements control structures:
if (test) { result }.
If the test is true, then the result is performed.
Otherwise, it is skipped.
If (test) { result 1}
else { result 2 }.
If test is true, result 1 is performed.
Otherwise, result 2 is performed. (Else must directly follow.)
if (test 1) { result 1}
else if (test 2) { result 2 }
else if (test 3) { result 3 }...
The first true test will have its results performed, and the rest will be ignored.
Adding an else at the very end is optional.
Boolean operators and/or/not.
|| or.
Binary, as it works on 2 statements.
&& and.
Binary.
! = not..
Unary, as it negates one thing.
Boolean operators short-circuit.
Or returns a true at the first true.
And returns a false at the first false.
Comparison operators > , < , >= , <= , == , !=
They are used for number primitives and characters.
They are not used for objects, as objects are more than single values.
2 Strings cannot be compared with ==.
This is because 2 strings may have the same value but they refer to different pieces of
memory, and thus would not be considered equal.
In order to check if s1 represents the same string as s2, one must use the equals method.
For example, s1.equals(s2).