Chapter 2 Java Building Blocks Flashcards
Creating Objects
How to create object?
- Calling Constructor
- Some classes provide built-in methods that allow you to create new instances without using a constructor or the new keyword. (Integer using the valueOf() method.)
Park p = new Park();
What is constructor?
A constructor in Java is a special method that is used to initialize objects.
The constructor is called when an object of a class is created.
It can be used to set initial values for object attributes.
Two key points to note about the constructor
2 key points to identify a constructor.
- Constructor name matches class name
- No return type
public class Chick { public Chick() { System.out.println("in constructor"); } }
Default Constructor
For most classes, you don’t have to code a constructor—the compiler will supply a “do nothing” default constructor for you.
The purpose of a constructor
is to initialize fields
READING AND WRITING MEMBER FIELDS
public class Swan { int numberEggs; // instance variable public static void main(String[] args) { Swan mother = new Swan(); mother.numberEggs = 1; // set variable System.out.println(mother.numberEggs); // read variable }
read values of already initialized fields on a line initializing a new field
1: public class Name { 2: String first = "Theodore"; 3: String last = "Moose"; 4: String full = first + last; 5: }
EXECUTING INSTANCE INITIALIZER BLOCKS
Code Block
The code between the braces is called a code block.(sometimes called “inside the braces” {})
Instance Initializer
Code blocks appear outside a method.
Line 5 is an instance initializer
1: public class Bird { 2: public static void main(String[] args) { 3: { System.out.println("Feathers"); } 4: } 5: { System.out.println("Snowy"); } //instance initializer 6: }
FOLLOWING ORDER OF INITIALIZATION
Order of Initialization
- Fields and instance initializer blocks are run in the order in which they appear in the file.
- The constructor runs after all fields and instance initializer blocks have run.
What is the output?
1: public class Chick { 2: private String name = "Fluffy"; 3: { System.out.println("setting field"); } 4: public Chick() { 5: name = "Tiny"; 6: System.out.println("setting constructor"); 7: } 8: public static void main(String[] args) { 9: Chick chick = new Chick(); 10: System.out.println(chick.name); } }
Ans : setting field setting constructor Tiny
1: public class Chick { 2: private String name = "Fluffy"; //2 3: { System.out.println("setting field"); }//3 4: public Chick() { 5: name = "Tiny";//4 6: System.out.println("setting constructor");//5 7: } 8: public static void main(String[] args) { 9: Chick chick = new Chick(); //1 10: System.out.println(chick.name); } }//6
Can you refer to a variable before it has been defined?
Ans : No.
Order matters for the fields and blocks of code. You can’t refer to a variable before it has been defined
Example :
{ System.out.println(name); } // DOES NOT COMPILE private String name = "Fluffy";
What do you think this code prints out?
public class Egg { public Egg() { number = 5; } public static void main(String[] args) { Egg egg = new Egg(); System.out.println(egg.number); } private int number = 3; { number = 4; }
Ans: 5
public class Egg { public Egg() { number = 5; //4 } public static void main(String[] args) { Egg egg = new Egg(); //1 System.out.println(egg.number); } private int number = 3; //2 { number = 4; } //3 }
Data types
- Primitive types
- Reference types
What is primitive types?
- Java has eight (8) built-in data types, referred to as the Java primitive types.
- A primitive is not an object in Java nor does it represent an object.
- A primitive is just a single value in memory, such as a number or character.
Balanced parentheses problem
You cannot use a closed brace “}” if there’s no corresponding open brace “{” that it matches written earlier in the code.
In programming, this is referred to as the balanced parentheses problem, and it often comes up in job interview questions.
Java primitive types
- boolean (true or false), defalult false
- byte (8-bit integral value), defalult 0 (-128 to 127)
- short (16-bit integral value), defalult 0 (-32,768 to 32,767)
- int (32-bit integral value), defalult 0 (-2,147,483,648 to 2,147,483,647)
- long (64-bit integral value), defalult 0L (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
- float (32-bit floating-point value), defalult 0.0f
- double (64-bit floating-point value), defalult 0.0d
- char(16-bit Unicode value), default ‘\u0000’ (or 0)
IS STRING A PRIMITIVE?
Ans : No.
What is String default value?
String or any object default value is null.
Java primitive type key points:
- The float and double types are used for floating-point (decimal) values.
- A float requires the letter f following the number so Java knows it is a float.
- The byte, short, int, and long types are used for numbers without decimal points. In mathematics, these are all referred to as integral values, but in Java, int and Integer refer to specific types.
- Each numeric type uses twice as many bits as the smaller similar type. For example, short uses twice as many bits as byte does.
- All of the numeric types are signed in Java. This means that they reserve one of their bits to cover a negative range. For example, byte ranges from -128 to 127. You might be surprised that the range is not -128 to 128. Don’t forget, 0 needs to be accounted for too in the range.
SIGNED AND UNSIGNED: SHORT AND CHAR
short and char are closely related, as both are stored as integral types with the same 16-bit length.
short is signed
char is unsigned
Unsigned means range is strictly positive including 0.
Therefore, char can hold a higher positive numeric value than short, but cannot hold any negative numbers.
Valid short and char declaration
short bird = 'd'; char mammal = (short)83; System.out.println(bird); // Prints 100 System.out.println(mammal); // Prints S
Invalid short and char declaration
short reptile = 65535; // DOES NOT COMPILE out of range char fish = (short)-1; // DOES NOT COMPILE out of range