byte short int
and long
are
integer types with size 1, 2, 4 and 8 bytes, respectively.sizeof()
operator, e.g.
sizeof(int)
or sizeof(a)
.float
is a floating point number which has a decimal point.double
has more decimal digits than float
and is used for accuracy.boolean
variables can have only two values: true
or false
; unlike in C they are not equivalent to 1 and 0.char
is a character (letter, number, punctuation, symbol,
or control character, e.g. 'a', '3', '!', '%', '\n').
int a;
a = 3;
int a = 3;
final float PI = 3.14;
abstract boolean break byte case catch char class const continue
default do double else extends final finally float for goto if implements
import instanceof int interface long native new null package private protected
public return short static super switch synchronized this throw throws
transient try void volatile while
PI, TAX_RATE
sales
, payRate
+ - * /
(addition,
subtraction, multiplication, division)%
is the modulus operator (integer remainder after division)== != < > <= >=
-x
or !x
x+y
and most
other operatorstest ? valueIfTrue :
valueIfFalse
if (mark >=50) grade = "pass"; else grade = "fail";
grade = mark >= 50 ?
"pass" :
"fail";
x = 3; a = x++; // a is set to 3, then x is incremented to 4
x = 3; a = ++x; //
x is incremented to 4, then a is set to 4
=
assigns a value to a variable, e.g. x = a;
assigns x the value of ax += 3;
is a short way of writing x = x + 3;
similar assignment operators include += -= *= /=
(TypeName) variable
(int) 3.14
converts the floating point number 3.14 to an integer 3int x;
while (x<10) { System.out.println(x); x++; }
// while x is less than 10, print and increment x
for (;;) System.out.println("hello"); // print hello forever
(press Ctrl-C to exit program)
/* C-style comment
can be on more than one line */
// C++-style comment is on one line
/** Javadoc comment (C-style comment which begins with two stars) */
System.out.println("text"); // print text and the newline character
System.out.print("text"); // print text; the following text will be on the same line
/**
get a string from the user using a dialog box
DialogString.java
@author Greg Vogl
2003-09-21
input: a string using a dialog box
output: the string
*/
import javax.swing.JOptionPane; // Swing is a Java SDK graphics library that contains the dialog box
public class DialogString
{
public static void main (String[] args)
{
String input = JOptionPane.showInputDialog("Enter a string");
System.out.println("You entered: " + input);
System.exit(0); // needed to exit a program with a GUI thread
}
}
/**
get a string from the user using the command line
ConsoleString.java
@author Greg Vogl
2003-09-21
input: a string using the command line
output: the string
@throws IOException to handle bad strings
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class ConsoleString
{
// main() must be able to throw an I/O exception to handle bad strings
public static void main (String[] args) throws IOException
{
System.out.println("Enter a string:");
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
String input = console.readLine(); // get a string from the user
System.out.println("You entered: " + input); // display the string to the screen
}
}