Download A Crash Course in Java

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
A Crash Course in Java
"Hello, World!" in Java
public class Greeter{
private String name;
public Greeter(String aName){
name = aName;
}
public String sayHello(){
return "Hello, " + name + "!";
}
}
constructor: Greeter(String aName)
method: sayHello()
field: private String name
public class GreeterTester
{
public static void main(String[] args)
{
Greeter worldGreeter = new Greeter("World");
String greeting = worldGreeter.sayHello();
System.out.println(greeting);
}
}
this class contains a main method, which means its runnable.
static: it doesn't operate on objects.
args: command-line arguments
If you want to run your program in a Shell Window:
go to your folder of your program,
javac filename.java => Run compiles
java filename => Start interpreter
output...
Documentation comments
/**
A class for producing simple greetings.
*/
public class Greeter {
/**
Constructs a Greeter object that can greet a person or
entity.
1
A Crash Course in Java
@param aName the name of the person or entity who should
be addressed in the greetings.
*/
public Greeter(String aName) {
name = aName;
}
/**
Greet with a "Hello" message.
@return a message containing "Hello" and the name of
the greeted person or entity.
*/
public String sayHello() {
return "Hello, " + name + "!";
}
private String name;
}
2
A Crash Course in Java
Primitive Types
The primitive Types of the Java Language
Type
Size
Range
int
4 bytes
-2,147,483,648 ... 2,147,483,647
long
8 bytes
-9,233,372,036,854,775,808L ...
9,233,372,036,854,775,807 L
short
2 bytes
-32768 ... 32767
byte
1 byte
-128 ... 127
char
2 bytes
'\u0000' ... '\uFFFF'
boolean
false, true
double
8 bytes
approximately ±1.79769313486231570E+308
float
4 bytes
approximately ±3.40282347E+38F
3
A Crash Course in Java
Character Escape Sequences
Escape Sequence
Meaning
\b
backspace (\u0008)
\f
form feed (\u000C)
\n
newline (\u000A)
\r
return (\u000D)
\t
tab (\u0009)
\\
backslash
\'
single quote
\"
double quote
\un1n2n3n4
Unicode encoding
Mathematical Methods
Method
Description
Math.sqrt(x)
Square root of x, √x
Math.pow(x, y)
xy (x > 0, ot x = 0 and y > 0, or x < 0 and y is an integer)
Math.toRadians(x)
Converts x degrees to radians (i.e., returns x . π/180)
Math.toDegrees(x)
Converts x radians to degrees (i.e., returns x . 180/π)
Math.round(x)
Closest integer to x (as a long)
Math.abs(x)
Absolute value |x|
Object References
Parameter Passing
Packages
Basic Exception Handling
Strings
Reading Input
ArrayLists and Linked Lists
Arrays
Static Fields and Methods
4
A Crash Course in Java
Programming Style
5