Programming Notes
Programming Notes
cd desktop
javac Name.java
java Name
************************
Java is case sensitive
In Java, class names starts with an uppercase letter
3 categories of identifiers:
**1st category: identifier that we made up:(names of these identifiers can be made
up of any
combination of letters, digits, special symbols. However, these names cannot start
with a digit
**2nd category: identifiers that someone else created (Examples include String,
System)
**3rd category: reserved words (have a special meaning): void, public, class
(start with lowercase letter)
3 types of errors:
- Compile-time error: a) syntax-related; b) detected by the compiler'
- Logical errors: when we don't get the intended result. In this case, code
compiles correctly.
Even worse, the code produces a result.
- Run-time errors: a) discovered by the interpreter during the execution. They are
also called
Exceptions.
Concatenation operator: +
By default, integer literal values like 12 are considered to be int value unless
you put an
l or L at its end to force Java to treat it as a long value. (12 is an int; 12L is
a long)
Data types:
Primitive data types (8)
byte, short, int, long ==> integer values (without decimal part)
float, double ==> decimal values
Unicode character set ---> 16 bits ---> 2^16 characters (approx. 65000 characters)
'0' ---> 48
'9' ---> 57
digits: '0' < '1' < '2' ... <'9' ....< 'A' (65) < 'B' < 'C' ... 'Z' (90) ... 'a'
(65 + 32) ... 'z'
Arithmetic Expressions:
% (remainder operator)
8 % 3 = 2
4 % 2 = 0
If either one of the operands or both are floating point values then the answer
will be a floating
point value.
2 + 3 ===> 5
2.0 + 3 ===> 5.0
Operator precedence:
14 + 8 / 2 ---> 14 + 4 ---> 18
(14 + 8) / 2 ---> 11
***********************************************************************************
******************
Increment + decrement operators
Data conversion:
a) Assignment operator:
int dollars = 23;
double money = dollars; // Widening conversion supported by the assignment operator
// dollars = money; // Narrowing conversion attempt ==> compile-time error
b) Promotion:
9.0 / 2 ===> 9.0 / 2.0 = 4.5
"hi" + 5 ===> "hi" + "5" ===> "hi5"
a) Widening conversion: you go from one type to another that uses more space (safe)
b) Narrowing conversions: you go from one type ot another using less space
Casting:
(double)
The cast operator has higher precedence than /
***********************************************************************************
*****************
import java.util.Scanner
Scanner offers a method called nextLine that has the following signature:
public String nextLine() ---->
a) String: type of method's output
b) nextLine: name of the method
c) (): empty list of parameters
To interact with the String object, we have to use the dot operator:
b) int len = str.length(); //length start from 1
2 variables pointing to the same object are said to be aliases of each other
I lost the last reference ==> This object becomes garbage (collected by garbage
collector)
We can force garbage collection in Java: System.gc();
**************indexOf
***********************************************************************************
******************
Random class (java.util)
Random rnd = new Random();
Challenge: use nextFloat to generate a random int value between 1 (inclusive) and 6
(inclusive)
[0.0; 1.0[ * 6 ---> [0.0; 6.0[ + 1 ---> [1.0; 7.0[ ---> (int) [1.0; 7.0[ --->
random int between
1 and 6.
***********************************************************************************
**********************
IMPPPPPPPPP:
***************
If the methods of the class are static ===> You just use the method through the
name of the class
***********************************************************************************
***********************
DecimalFormat ---> java.text
a) DecimalFormat fmt = new DecimalFormat("0.###");
b) fmt.format(value to be formatted)
- Wrapper classes:
There 8 wrapper classes
First approach:
Double valAsObj = new Double(val);
System.out.println(valsAsObj.intValue()); // 23
Second approach:
Double valAsObj = val; // Thanks to autoboxing:
// Autoboxing: automatic conversion between a primitive data type and
// the corresponding wrapper class
Unboxing:
Integer valAsObj = new Integer(23);
int val = valAsObj; // Unboxing
Automatic conversion between the wrapper class and its corresponding primitive data
type.
Example#2: JOptionPaneDemo.java