02. Java Basics
Java IdentifiersRules for Identifiers (names of variables, constants, classes, labels, methods):
- First character must be a letter ('a'-'z', 'A' - 'Z', or any Unicode** letter), underscore ('_') or dollar sign (')
- All other characters can be letters, digits ('0'-'9'), underscore or dollar sign
- Names are case-sensitive (e.g., FEET is different from Feet)
- Names are language-sensitive (e.g., Latin letters are considered different from corresponding Greek letters)
- All characters are significant
- The length is unlimited
- cannot be a Java keyword
Java DataTypes
Integral Types (values are pos. & neg. whole numbers and 0): int, short, long, byte
- There are no unsigned integers (unlike C)
- Integral literals are always stored as int unless otherwise specified
Floating Types (values can have a fraction and/or exponent): float, double (unlike C, there's NO long double type)
- Literals which have a decimal point or fraction are automatically stored as double, unless otherwise specified
- char Size: 16 bits, Min. value: \u0000, (\u is used to indicate a unicode value in hexadecimal-- see p. 5) max. value: \uFFFF
- Character literals are always enclosed in single quotes (see next page)
- Java uses the *Unicode character set to accommodate international characters. The first 127 values are the standard ASCII character set
boolean
- Java naming conventions:Class and interface names start with uppercase letter
- Method and variable names start with a lowercase letter
- If any name consists of more than one "word", then capitalize all words after the first (also known as "camel case")
- The only possible values are true or false (default value for a class-scope boolean variable is false)
- println() will display true or false (not 1 or 0, as in C/C++)
- No casting is allowed from boolean to int or vice-versa (unlike C/C++), so you cannot assign an int expression to a boolean variable nor a boolean expression to an int variable
BITWISE Operators
- Bitwise operators (like the C/C++ bitwise operators) manipulate data bit by bit (i.e., the corresponding bits of the operands are operated on) and may be applied only to integral operands: byte, short, int or long.
& bitwise AND (1 if both corresponding bits are 1)
| bitwise OR ( 1 if at least one corresponding bit is 1)
^ bitwise exclusive OR (1 only if one corresponding bit is 1)
<< left shift (shifts left operand the # of bits specified by right operand
>> right shift with sign extension
>>> right shift with zero extension
~ one's complement (unary: changes 1's to 0's and 0's to 1's)
/*
short num1, num2, mask;
// assume num1 has been assigned a number, doesn't matter what
Exercise 2.1 Write Java statements using the above variables to assign bits 4, 5, 6, and 7 (counting the left-most as bit 0) to num2, but in the least significant bits of num2 (bits 12, 13, 14 and 15). Hints: shift num1 so bit 4 is in position 12, then use a mask of 0x000F and the correct bitwise operator to get the result specified.
*/
public class Exercise_2_1
{
public static void main(String[] args)
{
short num1, num2, mask; // 0123 4567 8901 2345
num1 = 0x0573; // num1 = 0000 0101 0111 0011
mask = 0x000F; // mask = 0000 0000 0000 1111
num2 = (short) (mask & (num1 >>> 8)); // num2 = 0000 0000 0000 0101
System.out.println("num1: " + Integer.toHexString(num1) + "\nmask: " + Integer.toHexString(mask) + "\nnum2: " + Integer.toHexString(num2));
}
}
Arithmetic operators:
+ Addition
- Subtraction
* Multiplication
/ Division (integral result if both operands are integral)
% Modulus (remainder of integer division)
- (unary) Arithmetic negation + (unary)
Cast Operator:
- To change the type of an expression, you may give the type in parentheses in front of the expression: (type) expression
- Examples: d1 =(double) i1/i2;
i2 = (int)(d1+d2);
Shorthand Assignment Operators:
- Any operation with an assignment such as: var = var operator operand
- can be written as: var operator= operand
boolean Operators:
- NOTE: ALL boolean operators result in true or false, unlike C (NOT 1 or 0). Also, ALL boolean variables must be assigned a boolean expression (results in true or false, so unlike C, not any non-zero or zero value)!
Relational Operators:
- evaluate to true or false
OPERATORS:
< less than
> greater than
<= less than or equal to
>= greater than or equal to
== is equal to
!= not equal to
Logical Operators:
Operators
! Unary negation (NOT)
True if operand is False
&& Logical AND (short-circuited*)
True only if both operands are True
|| Logical OR (short-circuited*)
True if at least one operand is True
& Logical AND (both operands eval.)
True only if both operands are True
| Logical OR (both operands eval.)
True if at least one operand is True
^ Logical Exclusive-OR
True if ONLY ONE operand is True
Examples
!(a < b)
(ch>='a')&&(ch<='z')
(x==0) || (y==0)
(i1>0) & (i2>0)
i1++<10 | i2++<10
( n1!=0 ) ^ (n2!=0 )
/*Exercise 2.2 Change the following C/C++ code to legal Java code:
* int inum1=5, inum2=8;
* while( inum1-- ){ // OK in C, NOT in Java
* printf("%d", inum1);
* } // end while
*/
public class Exercise_2_2
{
public static void main(String[] args)
{
int inum1=5, inum2=8;
while( inum1-- != 0 )
{
System.out.println(inum1);
} // end while
}
}
/* Exercise 2.3 Change the following incorrect Java code (won't compile because you're trying to re-declare inum2):
int inum1=10, inum2;
for(int i=0, inum2=inum1; i < inum2; ++i, --inum2){
System.out.println("i = " + i);
} // end for
System.out.println("The last value of inum2 = "+inum2);
*/
public class Exercise_2_3
{
public static void main(String[] args)
{
int inum1 = 10, inum2;
inum2 = inum1;
for(int i=0; i < inum2; ++i, --inum2){
System.out.println("i = " + i);
} // end for
System.out.println("The last value of inum2 = "+inum2);
}
}
/* Exercise 2.4 Write Java code to print (lined up in columns) numbers from 2 to 20, their squares and their cubes, using a for loop (declare variables for the number as an int, and the square and cube as doubles). Use System.out.printf and right-justify the numbers.
*/
public class Exercise_2_4
{
public static void main(String[] args)
{
int i_num = 2;
double d_square, d_cube;
for(int i=2; i <= 20; i++, i_num++)
{
d_square = i_num * i_num;
d_cube = i_num * i_num * i_num;
System.out.printf("%d %8.2f %8.2f \n", i_num, d_square, d_cube);
}
}
}