Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Java notes

- Java is case sensitive>>>> naming Classes start with uppercase first letter
- The class of the main method MUST have the same name of the file containing it.
- To print in java, use System.out. println(“”);
- In java, codes are written inside classes ONLY.
- Package (folder): is a mechanism to encapsulate a group of classes, subpackages, and interfaces.
- System.out.print(): prints the string and leaves the cursor at the end.
System.out.println(): prints the string and moves the cursor to a new line.
System.out.printf(): format.
Explanation:
System is a class in java.lang package. Inside the System class, out is a static instance of class
PrintStream in java.io package. Inside the PrintStream class, println() is a method

- Java modifiers: access modifiers & non-access modifiers.


Access modifiers Non-access modifier

Class access levels: - final


- public - static
- default (Package private) - abstract
- volatile
Member access levels:
- public
- default (Package private)
- Private (Class private)
- protected
Note: when you need to add both access and non-access modifiers, the order will be the same as
mentioned above (access, then non-access modifiers) (Ex. public abstract class).
- Types of variables in java:
- Primitive variables: is a variable (Cup) full of bits representing the actual value of the variable.
- Object reference variables: is a variable (Cup) full of bits representing a way to get to the
objects.

- The 3 steps of object declaration, creation, and assignment:

1. Declaring a reference variable.


2. Creating an object: Instantiation and initialization.
3. Object assignment: link the object and reference.
- Any class that doesn’t explicitly extend another class, implicitly extends class Object.
-
C/C++ notes
- A class contains members that can be data members or member functions.
While a structure contains only data members.
- Dot operator: is a member access operator for an object variable of type class, struct, union.
- Arrow operator: is the same as the dor operator but used when the variable is a pointer, so instead of
using (*head).member = 12;
Use head->member = 12;
Because it combines the two actions of dereferencing the object and accessing the member.
- Defining structure:
struct optional tag which represents the struct name and ,commonly, removed for nested structures {
member declarations;
} optionally add the instance names here so that structure definition and instantiation happen together;
Instantiating structure:
struct tag instanceName;
- typedef: used to make new names for the types themselves such as int, double, struct tag.
Ex. typedef struct student student_data;
- GOOD note:
C C++

- Syntax for instantiating a struct variable: - Syntax for instantiating a struct variable:
struct structName instanceName structName instanceName
Note the use of struct keyword. - Syntax for instantiating an object of a class:
className instanceName
-
C/C++ vs Java
C/C++ Java

In C, “static” keyword is overloaded for two different Same as number three in C/C++
semantics:
1) for a global variable, makes it accessible only
inside the file in which it is declared.
2) for a local variable, makes it available through all
the program time, but visible inside its scope only
In C++, there is an additional third semantic:
3) makes data members and member methods
belonging to Class rather than an object.

- Meaning of “belonging to Class rather than an object”:


- You can access the member without creating an object of the class.
- you can access the member through the class literal, not through a reference or a pointer to the
class's instance.
- A single copy to be shared by all instances of the class.

You might also like