Java: Basic Concepts Flashcards
What happens in java when we write 7/10? Why?
7/10 in java will return zero. If we want to divide 7 by 10, then we must write 7.0/10 (which will return 0.7
Reason: 7/10 presents the problem as involving 2 integers. Java returns an integer because you asked for an integer. It runs the calculation, sees where the decimal is, and truncates the decimal. If you want it to return a float, you must use a float.
What is casting?
Typecasting is when one object reference can be type cast into another object reference.
Java has two casting scenarios. What are they?
Upcasting and downcasting
What is upcasting?
.Upcasting is when we cast an instance of a subclass to a variable of a superclass. Remember, an instance of a subclass is ALWAYS an instance of its superclass.
What is an implicit cast?
Instead of writing:
Object o = new Student(); m(o);
We can use an implicit cast:
m(new Student());
This works because an instance of Student is an instance of Object.
What is an explicit cast?
Sometimes we must explicitly tell the compiler that an object of its own class is in fact an object of its class.
Student b = o; [does not work]
Student b = (Student)o; [works]
We use this to downcast correctly.
What is downcasting, and why do we do it?
A downcast is when we cast an instance of a superclass to a variable of its subclass. We must use explicit casting to confirm our intention to the compiler with the (SubclassName) cast notation.
First, make sure the object to be casted is an instance of the subclass. use instanceof
Good analogy, pg 426. Fruit is a superclass. Apple and Orange are subclasses. We can easily assign an instance of Apple to a variable for Fruit. But because a fruit is not necessarily an apple, we must explicitly assign an instance of Fruit to a variable of Apple.
myObject.getDiamter [doesn’t work because the Object class doesn’t have getDiameter]
((Circle)myObject.getDiameter()); [DOES work because we told the compiler that the variable is an instance of a superclass, in this case, Circle]
What is a unary operator? Give example
A unary operation is an operation with only one operand. A negative number’s -x for instance
How does preincrement work, and why do we do it?
++var
Increments a variable and then returns that variable. (does not return the variable and then increment, as postincrement does)
If a variable does nothing but increment, then ++var and var++ are identical.
BUT
i = 1; int j = ++i; // j is 2, i is 2 [the new var is used in the statement]
i = 1 int j = i++; // j is 1, i is 2 [ the original var value is used in the statement]
Increments j by 1, then returns j
(whereas postincrement returns j, then increments j by 1)
How does predecrement work, and why do we do it?
–var
Decrements a variable and then returns that variable. (does not return the variable and then increment, as postdecrement does)
If a variable does nothing but decrement, then –var and var– are identical.
BUT
i = 1; int j = --i; // j is 0, i is 0 [the new var is used in the statement]
i = 1 int j = i++; // j is 1, i is 0 [ the original var value is used in the statement]
Decrements j by 1, then returns j
(whereas postdecrement returns j, then increments j by 1)
What is a literal?
A literal is the value assigned to a variable.
double weight = 0.305;
the literal is 0.305
What are augmented assignment operators?
+= -= *= /= %=
example:
i += 8
i = i + 8
these are the same
Describe the char data type
a single 16-bit unicode character
Describe what a string is
A string is an object from the string class.
A string is immutable
A string is “changed” through methods that actually make a new string object
What does \t do
inserts a tab in the text at this point
what does \b do
inserts a backspace in the text at this point
what does \n do
inserts a newline in the text at this point
what does \r do
inserts a carriage return in the text at this point
what does \f do
inserts a formfeed in the text at this point
what does ' do
inserts a single quote character in the text at this point
what does " do
inserts a double quote character in the text at this point
what does \ do
insert a backslash character in the text at this point
what does next() do
Part of Scanner class. Finds and returns the next complete token from this scanner. whitespace is the default delimiter.
this is called a token reading method
what does nextInt(), nextFloat(), etc do
Scans the next token of the input as an int, double, etc (read the Scanner class for more)
this is called a token reading method