A variable declaration has three basic parts.
TYPE NAME = VALUE;Where TYPE corresponds to a type in java, like String, int, bool...
and NAME corresponds to a name we give a variable. Lastly, VALUE
represents a valid value for our type. This is important. I can't for example
say
int x = "dlkjslkjd";Because we declared TYPE to be int yet we provided a VALUE
which could only be interpreted as a String.
Q1: Give a valid declaration of an integer, a string and a scanner
We also have a modifier which we can put on a type. We have seen two examples
of this. First, is final, which says that we won't change the type:
final double PI = 3.1415Next we have seen that, appending [] to our type says that we want an array
of that type, rather than just one object of it:
int[] phoneNumbers = { 4089993333, 9254449088 }Q2: Declare a constant array of names of cities
We have seen two types of types in java. First are what we call primitive types, these include:
boolean
char
int
float
doubleThese aforementioned types are built in, provided by java along with a set of operations we can use on them, such as +,-./,* for ints, and &&,|| for booleans.
We have also seen class types, these include:
Scanner
StringThese are created using the java new operator:
Scanner s = new Scanner(System.in);They also allow us to call operations on them.
int i = s.nextInt();Q3: What type is int[]
Math operators are operators which work on double, int and float.
These include + - / and *. Remember that
we can use these in conjunction with assignment, like this
int i = 0;
i -= 2; // now i is -2
i += 2; // now i is 0
i /= 10; // now i is 0! remember integer division truncates!
i *= 123;// now i remains to be 0
i++; // yay, i is 1.We also have logical operators, they are of the form bool OPERATOR bool.
For example
true && falseThe and operator && returns true if both of its arguments are true
and false otherwise. The or operator || returns true if either
of its arguments are true, and false otherwise.
We also have concatenation for strings. All this means is combining two strings.
We have seen this many times when we print things:
String name = "Jim";
System.out.println("Hello, " + name); Here, the + operator is joining(also called concatenating) the two
strings "Hello, " and "Jim" together into one string and passing that
to the printing function.
if, else and else if are statements that take a logical expression(Something that evaluates to a boolean value, like (3 < x) decides whether or not to execute its body based on that truth value.
One things to watch out for is overlapping the logic in if and else if statements
int x = 2;
if(x == 2) {
// do something
}
else if(x > 0) {
// do something else
}See, the problem here is that BOTH of these statements will be true, so both of them will be executed, which is not typically what we want. Make sure our cases are distinct.
for, while and do while loops allow us to do something many times. We have seen a number of these, they are of this form:
for(INTITIALIZATION; CONDITION; INCREMENT) {
BODY
}
while(CONDITION) {
BODY
}
do {
// BODY
} while(CONDITION);Q4: What is the difference between do while and while? What is the difference between for and while?
We can declare methods inside of a class.
public class Example {
public static int count(String s) {
return s.length()
}
public static void main(String[] args) {
String s = "abc";
int i = count(s);
}
}We can use methods by simply calling their name as above(count(...)).
Q5: What is the return type of count, what is the type of the parameter?
Q6: Write the function signature for a reverse function
Remember, a scanner object is simply something that listens for input. When we call its methods, it will look for input of a certain type.
Scanner s = new Scanner(System.in);
int i = s.nextInt();Here we are listening for the next input to the console, and trying to
assign its value to our int variable. We will run into problems if the
user were to enter a number such as 2.3. So we must be cautious
by telling the user how to input things.
Q7: Below is some code with four control structures(for, while etc..) in the main method. Your job is to figure out what it prints.
public class controlflow {
public static void main(String[] args) {
// (1) What will this output ?
System.out.println("Question 1 ================= ");
for(int i = 0; i < 10; i+=4) {
// (1a) What is this if-statement
// checking?
if(i%2 == 0) {
System.out.println("XXX");
}
}
// (2) Ok, try this one
System.out.println("Question 2 ================= ");
int i = 0;
do {
System.out.println("ZZZzzZZZ");
i++;
} while(i < 5 );
// (3) Last one, I promise
System.out.println("Question 3 ================= ");
for(int j = 0; j < 5; j++) {
for(int k = 0; k < 5; k++) {
for(int l = 0; l < 5; l++) {
if(l == j || j == k) {
System.out.println("HELLOWORLD");
}
}
}
}
// (4) Ok, I lied. One more.
System.out.println("Question 4 ================= ");
i = 0;
while(i < 15); {
System.out.println("***");
}
}
}A1:
int i = 7;
Scanner s = new Scanner(System.in);
String s2 = "Hellllllooooooo World";A2:
final String[] cities = { "San Jose", "Oakland" };A3: It is a class type, since we used the new operator
to create it, and it was therefore created on the heap and not
the stack.
A4: do-while guarantees that it will execute its body once, but while might not ever execute its body if the condition fails on the first go-around.
for has the initialization in the statement, but in a while, we have to do it separately.
For example:
int i = 0;
while(i < 10) { i++; }
// versus
for(int i = 0; i < 10; i++) { }A5: count has a return type of int and a parameter type of String.
A6:
public static String reverse(String s)A7:
Question 1 =================
XXX
XXX
XXX
Question 2 =================
ZZZzzZZZ
ZZZzzZZZ
ZZZzzZZZ
ZZZzzZZZ
ZZZzzZZZ
Question 3 =================
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
HELLOWORLD
Question 4 =================
Notice that the 4th example hangs and will never complete. That is because
there is a tricky semicolon ; next to our while(...) expression, so
we have an infinite loop.
Remember that these two are equivalent:
int i = 0;
while( i < 10); {
i++;
}int i = 0;
while (i < 10 ) {
}This is because a ; is a statement in Java. It is an empty, pointless one
and no-doubt a source of bugs in our programs, so we must learn to watch out for it.
This is a great study guide, thanks for putting this together!