Chapter 1: Java Building Blocks Flashcards
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.
What does it mean to initialize a field?
It means to give the variable its first value.
What is a reference type?
A type of data that refers to an object (which is an instance of a class)
What is made when you compile a .java file?
Bytecode under the name of .class
Why do you need an f on the end of a float?
So that Java knows you are using a float instead of a double. You need to do this for longs as well
What is a method?
An operation that can be called to do something to the state of a program.
Where do method declarations go?
any where inside a class
Is java an interpreted language?
Yes, because it compiles into bytecode
What’s a key advantage of java over C++?
Memory management, C++ requires you to tell it when to collect garbage so it is prone to mistakes
What does java do with bits?
figure out how much memory it needs to reserve
What does the acronym PIC stand for?
Package, imports, class. It’s to help with remember the order.
How can all references be the same size?
References are not the object that they point to, so they will always be the same size.
What is a variable?
A name for a piece of memory that stores data
Can objects be passed to a method or returned from a method?
No. Only an object’s references can be passed around
How often could the finalize() method run (if put on the class one time
0 or 1 times. Never 2
What do variables do?
Hold the state of the program/make the class unique compared to another instance of the class
What does variable scope mean?
It refers to the fact that variables only contain data to be passed around for however long the method/class is around. If you are outside of scope you cannot call the method.
What is a parameter?
It’s a piece of a method. If you call a method that requires a parameter you must pass in a parameter matching the type of that named in the method declaration.
What is a method signature? (Three things)
The full declaration of a method which includes: 1. Method permeability (private? public? protected?) 2. return type (void? String? Integer?) 3. Parameter list or requirements.
What three rules do you have to remember for legal identifiers?
- The name must start with a letter or the symbol $ or _ 2. Subsequent characters may be numbers 3. You cannot use a java reserved word
What does platform independent refer too?
The fact that java is ‘write once, run everywhere’. The same code gets compiled into the same byte code on every machine.
Describe the key words: public void getName()
public means it can be called by other classes. void means when the method is called it will not return a value. After the method runs it returns control to whatever class called it.
What does the finalize() method do?
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. Simply put, this is called before the garbage collection for a particular object. This method only runs if the garbage collector gets called.
Explain what’s happening here: String greeting = “How are you?”
We’ve created a reference called ‘greeting’ that points to an address where a String object with the data, “How are you?” is stored.
Do constructors have return types?
no, for example you would never see public void MyClass(){}