Chapter 2 Flashcards
What is a constructor in java
this is a special type of method that creates a new object based on the parent Class.
What is the correct syntax of a constructor within in a class
public name () { }
What does a normal method have that a constructor does not have
A return type
What do we know of the name of a constructor class
The name matches the name of the parent class.
what is between the braces in java { }
a Code block
Name four types of code blocks
A Class definition
A Method declaration
A Inner Block
A instance initializer
What is the correct order of initialization in a class in regards to running the code
Fields and instance initializer first in order of appearnce in the code (top to bot) Followed by the constructor.
is a “String” variable a primitive type?
No, A string is a object, not a primitive type.
What are correct values for a boolean
True or False
What is the correct value range for a byte
-128 to 127
what is the difference between a int and a float
The same range as the fields of a int, but with up to 7 decimal digits
what is the difference between a long and a double
The same range as the fields of a long, but with up to 16 decimal digits
what do all possible values of the ‘char’ type have in commen.
They are all members of the Unicode Ascii values
What is a easy way to calculate the total range of a “byte”
A byte is 8 bits. A bit can have 2 values : 1 or 0. So a byte can have a range of 2^8 different combinations of bits.
How are the maximum number of bits used by the Java engine.
Java uses the bits to figure out how much memeory is needed for a Var. For example Java allocates 32 bits if you write (int num;) Memory is wasted if the value of num never exceeds a “short” wich is halve the size.
What happens when we you declare a value that fits a long and does not fit a int. But without including a capitol L at the end of the value.
java will throw a error because any number in java code is considered a int if not declared as a long by adding the L like : 3123456789L
Where can you NOT use a underscore inside a number decleration.
Not at the start 001.0
Not at the end 001.0
Not next to a decimal. 001_.0
This is fine : 0_01.0 or 001_0
will this compile :
String reference = “hello”
int len = reference.length();
int bad = len.length();
It will not compile.
len is a primitive data type that we can not use to call methods. The method length can be called on the string because a string is not a primitive.
what is the difference between declaring a variable and initializing a variable.
A declared variable is not used yet, the java problem is simply aware of the variable and it can be initialized. Initializing the variable is the first time a value is assigned to this.
What is the correct syntax to declare and initialize a variable
=
String zooName = “Arits”;
or
int numberAnimals = 100;
what is a java identifier
this is the name of a variable method class interface package
what does a java identifier have to begin with
a letter : example
a $ symbol $example
a _ symbol _example
what are the special rules regarding numbers in identifier
they can be used in a identifier but can not be used as the first character.
not valid : 1example
valid example : example2
Can a single _ (underscore) be used as a identifier
no, this is not allowed in versions after java 9
can you use java reserved words as identifiers?
No you can not
Since java is case sensitive you could use “CLASS” but you can’t use “class”
Don’t do this, even if you can
what are the allowed characters to be used in a identifier
the only allowed characters for identifiers are all alphanumeric characters([A-Z],[a-z],[0-9]), ‘$‘(dollar sign) and ‘_‘
what are the common conventions around camelCasing in identifiers
method and variables are writtin in camelCase with the first letter being lowercase
likeThisExample
class and interface names are written in camelCase with the first letter being uppercase
LikeThisExample
how many variables are declared and initialized in this statement { String s1, s2; String s3 = "yes" , s4 = "no"; int i1, i2, i3 = 0; }
4 are declared
s1, s2, i1, i2
3 are initialized
s3, s4, i3
which of the following are legal declarations 1 boolean b1, b2; 2 String s1 = "1", s2; 3 double d1, double d2; 4 int i1; int i2; 5 int i3; i4;
1 : legal
2 : legal
3 : NOT LEGAL - you can’t declare 2 var types in the same statement even if they are the same types.
double d1, d2; would be legal
4 : legal
5 : NOT LEGAL - this line has 2 statements, and one does not have a var type.
int i3; int i4; would be legal
When must a variable be initialized
BEFORE using it.
where can you use a local variable
inside of a constructor, method or initializer block
where are instance variables used
this variable is part of one object.
where are class variables used
This variable can be used by all instances of a class. It can also be used outside of the class without a active instance of the class.
does this compile public class VarKeyWord { var tricky = "Hello"; }
NO, that will not compile
tricky is a instance variable and the var keyword can only be used for local variables.
what happens when you use the var keyword as a variable type when declaring and initializing a variable.
This can only be done for local variables and it tells java to infer the type of the variable based on the first value.
Can a local variable that has been initilized using var change its type
no, it cannot.
does this compile
{
var question;
quesiton = 1;
}
No. To declare a var variable it has to be initialized in the same statement. this would compile: var question = 1; or var question = 1;
why does this not compile
public int addition (var a, var b){
return a + b
}
because the var keyword can not be used for method parameters. It can only be used for a local variable
does this compile public class var { public var() { } }
Does not compile var is not a reserved word, but it is a reserved type. So it can not be used to to define a type such as class interface enum
when does a local variable go out of scope
At the end of the code block where the variable has been declared.
when does a instance variable go out of scope
when the parent object is eligible for garbage collection
when does a class variable go out of scope
in scope from declaration until program ends