Chapter 1: Java Building Blocks Flashcards
(120 cards)
Which value type and size for an int?
32 bit integral value
Which value type for a double?
64 floating point value
Is this allowed? double d1, double d2;
No. If you want to declare multiple values of the same type you cannot repeat the type in front of each variable name. This would work however if coded like this: double d1; double d2;
What’s the default value for a boolean?
false
What values are possible for a boolean?
true or false
what’s the default value for a char?
‘\u0000’ (NUL)
Read/Write/Get/Set example
String hi = “hi”;//set/write String by = “by”;//set/write String both = hi + by;//set/write and get/read System.out.println(both); //get/read
How many primitive types come built in to java?
8
What can happen if you have a number with many digits (non floating point)? How can you prevent a compiler error for a number like 213123123123?
It means a number out of range for an int, even if it is called primitive type long, won’t be registered as a long unless it has an l at the end of it. long max = 312345231233; //does not compile long max - 123312341234L; //now java knows it is a long
What does it mean to ‘set’ a variable?
To write it
Does this work (comparing the two Date classes): import java.util.*; import java.sql.*;
no. Compiler error because which Date class to be used is ambiguous. You can fix this by telling it to pick a precedent: import java.util.Date; import java.sql.*; if you really need to use both Date classes you can be explicit with the full name for the class in the application. like table names in sql
What is a pointer?
A reference type; pointers (references) point to a place in memory where an object is stored.
Which of these are valid identifiers? identifier $identifier _identifier __indetifier$$ 3DPointClass hollywood@vine *$Coffee public
$identifier - ok _identifier - ok __indetifier$$ - ok 3DPointClass - not ok, identifiers can’t start with a number hollywood@vine - not ok, @ symbol is not a letter, digit, $|_ *$Coffee - not ok, *, not a letter digit $ or _ public - not ok, this is a keyword
When do constructors run?
After all the fields and instance initializer blocks have run
Which value type and size a short?
16 bit integral value
Does this compile? public int method(){ int y=10; int x; int reply = x+y; return reply; }
No, local variables must be initialized or they throw a compile time error.
Do objects get garbage collected or references?
Objects
Which can be assigned null: reference types or primitive types?
Reference types
When are field and instance initializer blocks run?
In the order in which they appear in the file. Fields and instance initializer blocks run before the constructor.
does main() always run first?
yes
What could be consider members of a class?
methods and fields
Must instance/class variables be initialized by me?
no. They are given a default value. Remember, the compiler always wants to keep things as simple as possible : null for an object and 0/false for primitives
Can you put two classes in one .java file?
Yes, but only one of the classes is allowed to be public.
Do local variables have a default value?
No.