Java Reference
In-Depth Information
int i = (int) 32.94
⇒
Converts the number 32.94 to an integer. The result is
always rounded down. The integer
i
will become equal to 32.
int i = 2/3
⇒
The result of integer division is an integer. The result is rounded
down. Therefore, the variable
i
will become equal to 0.
The variable
f
will become equal to approximately 2/3. The
reason is that the result of dividing an integer by a double is a double.
⇒
double f = 2/3.0
⇒
Exists a method.
return
int a = Math.max(b,c)
⇒
Finds the max of two numbers.
int a = Math.min(b,c)
⇒
Finds the min of two numbers.
⇒
int a = Math.abs(b)
Finds the absolute value of a number.
⇒
Finds the square root of a double.
double a = Math.sqrt(d)
2.10 Important Points
1. A file called
Foo.java
must include exactly one public class called
Foo
.
2. Every Java program has a
main
method where the program starts executing.
3.
Debugging
is executing a Java program line by line and monitoring the values of the
variables. Use debugging to isolate errors in your code. Before debugging in
NetBeans
,
first set a
breakpoint
. This is where the program will stop executing and where the
debugging will start.
4. The block of every method starts with an opening brace and finishes with a closing
brace.
5. The closing brace of a block should appear exactly under the line of the opening brace.
Select the
Source
-
Format
menu in NetBeans to auto-format the code.
6. Add a semicolon (a.k.a., ;) after every statement. However, do not add a semicolon
after an
if
or a
switch
statement.
7. Give thought to variable names. Do not name variables
x
and
y
when less generic
names are more appropriate.
8. Every variable must start with a lowercase letter. Conversely, the name of every class
starts with a capital letter.
9. Every variable has a
scope
. This is where the variable is defined. The scope of a variable
starts from the point where it is defined and ends with the end of the innermost block.
The start of a block in Java is denoted by an opening brace, while its end is denoted
by a closing brace.
10. The left side of an assignment operator (i.e., =) must be a variable. For example,
x
=4.