++, -- | arithmetic, char | increment, decrement |
+, - | arithmetic | unary plus, negation |
~ | integral | bitwise complement |
! | boolean | logical complement |
(type) | any | cast |
Tip: Avoid casting. If it is safe, then it is not needed. Unfortunately, OO programming requires narrowing between class types occasionally. Avoid instanceof. It is normally not needed. OO programming and dynamic dispatch is better. Exception handlers do this case analysis for you too, don't do it yourself with instanceof.
*, /, % | arithmetic | L | multiplication, division, remainder |
+, - | arithmetic | L | addition, subtraction |
+ | string | L | concatenation |
<< | integral | L | left shift |
>> | integral | L | right shift with sign extension |
>>> | integral | L | right shift with zero extension |
<, >, <=, >= | arithmetic, char | L | less than, greater than, less than or equal, greater than or equal |
instanceof | object x type | L | examination of type |
==, != | primitive | L | equal (and not equal) values |
==, != | object | L | equal (and not same) references |
&, ^, | | integeral and boolean | L | bitwise (and logical) and, xor, or |
&&, || | boolean | L | conditional and, or |
All the binary operators associate to the left. There is no exponentiation operator. See the class for other arithmetic operations including the method .
Note that the "rem" operator '%' is not mathematical modulus and may return a negative.
x -8 -7 -6 -5 -4 -3 -3 -1 0 1 2 3 4 5 6 x mod 4 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 x rem 4 0 -3 -2 -1 0 -3 -2 -1 0 1 2 3 0 1 2
x -8 -7 -6 -5 -4 -3 -3 -1 0 1 2 3 4 5 6 x mod -4 0 -3 -2 -1 0 -3 -2 -1 0 -3 -2 -1 0 -3 -2 x rem -4 0 -3 -2 -1 0 -3 -2 -1 0 1 2 3 0 1 2
Tip: Avoid instanceof. It is normally not needed. OO programming and dynamic dispatch is better. Exception handlers do this case analysis for you too, don't do it yourself with instanceof.
Tip: If you find yourself using bitwise operations often, then consider using the java.util.BitSet class.
There is one 3-ary operator ?: just like in C++.
System.out.println (a==null?"impossible":a.toString()); bestsum[r][c] += above > right ? above: right; final int max = args.length>0?Integer.parseInt(args[0]):100; m[i][j] = (i==j)?1.0:0.0; // diagonal out.setRGB (row, col, (row+col)%2==0?WHITE:BLACK); // checkerboard
Unlike C++ there is no reference and dereference operators *, ->, and &. Futhermore, Java doesn't consider array access or field access to be operators.
String conversion: String conversion applies to the operands of the binary + operator when one of the arguments is a String.
String + int float + StringIn this special case, the other argument is converted to a String and both strings are concatenation to form the result. The conversion works for all types (primitive and reference).
See the example Block.java. See also Print.java.
1 + 2 + "fiddlers" // yields "3 fiddlers" "fiddlers " + 1 + 2 // yields "fiddlers 12"
String.format ("%s=%d", "key", 3)Tip: Always implement the toString for classes that get instantiated.
class C { static final double RADIUS = 6378.145 // average radius of earth in km }
Consider the problems with this program Local.java.
int[] ar; // declaration of an array variable String ar[]; // declaration of an array variable float ar[] = new float [509]; // array 509 integers defined String names[] = {"Tom", "Dick", "Harry"}; // array and elements inialized
All array subscripts start at zero.
It is a common mistake to forget to allocate space for an array.
int[] A; A[3] = 3; // NullPointerExcepton int[] B = new int [4]; B[3] = 3; // OKArrays declared to be final are quite common.
final int[] C = new int [4]; C[3] = 3; // OK; array variable C is not changed
You can create arrays to pass
double[] arrival = new double [] {a0, a1, a2, a3}; setArrival (arrival); setArrival (new double [] {a0, a1, a2, a3});Or return ...
public static double [] pair () { final double x = 1.0; final double y = 2.0; return new double [] {x, y}; }
int matrix[][] = new int [7][5]; // 7×5 two-dimensional array matrix.length==7 matrix[0].length==5 int[][] matrix = {{1,2}, {3,4}, {5,6}}; // 3×2 two-dimensional array int[][] Tri = {{},{1},{1,2},{1,2,3},{1,2,3,4}}; Tri[0].length!=Tri[1].length
Strings of characters are an important data type in programming, often used in representing natural language text.
Strings are immutable: no operations can change a string. If you need to change a part of a string see the Java class (new in Java 1.5), or the synchronized (hence slower), older class.
The Java class has many methods for working with text. Many of these methods are used frequently. Here is a table listing some of the most common methods.
char | |
int | |
int | |
boolean | |
boolean | |
boolean | |
boolean | |
boolean | |
int | |
int | |
int | |
int | |
int | |
String | |
String | |
String | |
String | |
String | |
String |
In conjuction with text, regular expressions are important. See the Java regular expression package .
An invocation of this method of the form str.split(regex,n) yields the same result as the expression
Pattern.compile(regex).split(str, n)
As of Java 1.4 an assert statement has been added to the language.
As of Java 1.7 a try-with-resources statment has been added.
try (final BufferedRader br = new BufferedReader (new FileRader(path))) { return br.readLine(); }