General knowledge Flashcards
How is data passed in Java?
Arguments in Java are always passed-by-value. During method invocation, a copy of each argument, whether its a value or reference, is created in stack memory which is then passed to the method.
Primitive variables store the actual values, whereas Non-Primitives store the reference variables which point to the addresses of the objects they’re referring to.
Whenever an object is passed as an argument, an exact copy of the reference variable is created which points to the same location of the object in heap memory as the original reference variable.As a result of this, whenever we make any change in the same object in the method, that change is reflected in the original object.
Primitive variables are directly stored in stack memory. Whenever any variable of primitive data type is passed as an argument, the actual parameters are copied to formal arguments and these formal arguments accumulate their own space in stack memory.
The lifespan of these formal parameters lasts only as long as that method is running, and upon returning, these formal arguments are cleared away from the stack and are discarded.
*The reference variables are stored in stack memory and the object that they’re referring to, are stored in a Heap memory.
https://www.baeldung.com/java-pass-by-value-or-pass-by-reference#:~:text=Both%20values%20and%20references%20are,then%20passed%20to%20the%20method.
**All the wrapper objects are immutable!!!!!!!!
So when you do obj++, what actually happens is something like this:
obj = new Integer( obj.intValue() + 1);
How many primitive types are in Java?
The 8 primitives defined in Java are int, byte, short, long, float, double, boolean and char. These aren’t considered objects and represent raw values.
Type Size (bits) Default {min,max} Example
byte 8 0 {-128;127} byte b = 100;
short 16 0 {-32,768;+} short s = 30_000;
int 32 0 {-2.1…mild} int i = 100_000_000;
long 64 0 long l = 100_000_000_000_000;
float 32 0.0 float f = 1.456f;
double 64 0.0 double f =1.456789012345678;
char 16 /u0000’ char c = ‘c’;
*char is a 16-bit integer representing a Unicode-encoded character. Its range is from 0 to 65,535. In Unicode, this represents ‘\u0000’ to ‘\uffff’
boolean 1 false boolean b = true;
types of data representation in Java
primitive types and reference types.
How can object be created in Java?
- With the “new” keyword” –> responsible for allocating memory for the new object through a constructor.
If we don’t supply a constructor explicitly, the compiler will create a default constructor which has no arguments and just allocates memory for the object.
A class can have many constructors as long as their parameters lists are different (overload). Every constructor that doesn’t call another constructor in the same class has a call to its parent constructor whether it was written explicitly or inserted by the compiler through super().
Types of scopes that a variable in Java can exist within and how this affects the initialization process.
- Instance and Class Variables
don’t require us to initialize them. As soon as we declare these variables, they are given a default value as follows:
boolean –> false
Byte,short, int,long –> 0
float,double –>0.0
char –>’ /u0000’–> empty chracter?
Reference type –> null
- Local variables
must be initialized before use!
*
The Final Keyword
The final keyword applied to a field means that the field’s value can no longer be changed after initialization. In this way, we can define constants in Java.
Initializers in Java
In Java, an initializer is a block of code that has no associated name or data type and is placed outside of any method, constructor, or another block of code.
Java offers two types of initializers, static and instance initializers.
Order of Initialization
- static variables and static initializers in order
- instance variables and instance initializers in order
- constructors
Object Life Cycle
he garbage collector is a Java program that takes care of automatic memory management by deleting objects that are no longer reachable.
For a Java object to become unreachable, it has to encounter one of the following situations:
The object no longer has any references pointing to it
All reference pointing to the object are out of scope
Overload
A method is said to be overloaded when the other method’s name is same and parameters are different ( either the number or their order) .
The return type does NOT matter!
An overriding method (in a extending class)can change the return type to a subclass of the return type declared in the overridden method. But remember than covarient returns does not apply to primitives.
What does “continue” and “break “ do
Continue - stops and goes to the next iteration
Break - stops the loop
Compile errors
If a statement is not reachable–> it will cause a compile-time error
EG:
while (false) { x=3; }
EXCEPTION:
In if(false){ x=3; }, although the body of the condition is unreachable, this is not an error because the JLS explicitly defines this as an exception to the rule. It allows this construct to support optimizations through the conditional compilation. For example,
if(DEBUG){ System.out.println(“beginning task 1”); }
Here, the DEBUG variable can be set to false in the code while generating the production version of the class file, which will allow the compiler to optimize the code by removing the whole if statement entirely from the class file.
Characteristics of JAVA
Is a high-level language that can be characterized by all of the following buzzwords:
Simple
Object oriented
Distributed
Multithreaded
Dynamic
Architecture neutral
Portable
High performance
Robust
Secure
source code in .java –> compiled into .class files(it contains bytecode native tot he JVM) which can run on many different operating systems that run JVM
polymorphism
Is achived throw:
overriding
and
overloading
Advantages:
**It makes the code more reusable
**It makes the code more dynamic(Polymophism allows the actual decision of which method is to be invoked to be taken at runtime based on the actual class of object. This is dynamic binding and makes the code more dynamic)
Disadvatages:
polymorphism causes a very slight degradation of execution efficiency due to dynamic binding at run time.