


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



















About Java
Variable Names
Characters
number between 0 and 255
char c1 = ‘a’;
System.out.println(c1 + 2);
int c2 = c1 + 2;
System.out.println(c2);
char c3 = (char) (c1 + 2);
System.out.println(c3);
c
Arithmetic
and ++num?
System.out.println(num++);
Objects
can call methods on them
String d = scanner.next();
Object’s data is. Primitives actually save the
value of the variable
Character class
provide static methods
that you can call with the
class name
You cannot instantiate
them!
Math math = new Math();
a 5
s
Dan
Dan
Strings
String s = “1\t2\n\t3\t\”;
System.out.println(s);
Strings
double
String s = “72”;
int a = Integer.parseInt(s);
double d =
Double.parseDouble(“38.08”);
int b =
Integer.parseInt(“seven”);
int a = 7;
String s = a + “”;
Strings, never use ==. The ==
operator compares the values at
the memory address. Objects
store a location in memory
where the data is stored, not the
actual value of the variable. Use
equals instead
equalsIgnoreCase()
Crashes!
Scanner Example
“First 3 24.8 12 Last”
Scanner s = new Scanner(“First 3 24.8 12 Last”);
s.next();
s.next();
s.nextInt();
s.nextDouble();
s.nextInt();
s.next();
“First”
crashes!
Last
s.hasNext();
false
Math Class
pow
double d1 = Math.pow(2, 3);
round
double d2 = Math.round(7.8);
ceil
double d3 = Math.ceil(2.1);
double d4 = Math.ceil(3.0);
floor
double d5 = Math.floor(5.6);
sqrt
double d6 = Math.sqrt(34);
Printf Examples
int a = 7;
System.out.printf(“%3d\n”, a);
System.out.printf(“%03d\n”, a);
double d = 3.2;
System.out.printf(“%04d\n”, d);
System.out.printf(“%.3f\n”, d);
System.out.printf(“%06.3f%%\n”, d);
System.out.printf(“My name is %s\n”, “Dan”);
double d2 = 1234567.8;
System.out.printf(“%f\n”, d2);
System.out.printf(“%2f\n”, d2);
crashes!
“My name is Dan”
If Statements
want to change the logic of your
program based on values of
variables or properties of Objects
evaluates to Booleans
next statement unless curly
braces are used to create code
blocks, regardless of indentation
if (a == 3)
System.out.println(“equal”);
System.out.println(“always runs”);
a = 3;
“equals”
“always runs”
a = 2;
“always runs”;
if (a == 3)
System.out.println(“equal”);
else
System.out.println(“not equal”);
a = 2 a = 3
If Statement Examples
int a = 2, b = 3;
if (a == 2 && b == 3)
System.out.println(1);
else if (!(a != 2 || b != 3))
System.out.println(2);
This will never be called,
no matter what a and b
are. Why?
(a == 2 && b == 3) is the same as !(a != 2 || b != 3)
if (a == 8 && b == 5)
System.out.println(“a is 8”);
System.out.println(“b is 5”);
Misleading indentation!
Only executes next statement!
a = 2, b = 3
if (a == 8 && b == 5)
x++; y++;
a = 8, b = 5
if (a == 8 && b == 5) {
System.out.println(“a is 8”);
System.out.println(“b is 5”);
a = 2, b = 3 a = 8, b = 5
a = 8, b = 5
a = 8, b = 5
a = 8, b = 5
a = 2, b = 3
a = 2, b = 3
If Statement Examples
if (a <= 5)
if (b <= 2)
Sysytem.out.println(“tiny”);
else { System.out.println(“huge”);
System.out.println(“end”); }
Misleading indentation! Rewrite it
if (a <= 5)
if (b <= 2)
System.out.println(“tiny”);
else
System.out.println(“huge”);
System.out.println(“end”);
else goes
with
the most
recent if
a = 2, b = 1a = 2, b = 3
a = 6, b = 1
if (a <= 5) {
if (b <= 2)
System.out.println(“tiny”);
else
System.out.println(“huge”);
System.out.println(“end”);
This is the same thing!
Just clearer (and better)
if (a <= 5) {
if (b <= 2)
System.out.println(“tiny”); }
else
System.out.println(“huge”);
System.out.println(“end”);
Misleading indentation! Rewrite it
a = 2, b = 1 a = 6, b = 1a = 2, b = 5