Java Flashcards
public static void main(String[] args)
“public” is the access modifier which specifies from where and “who” can access the program
“static” makes it so that the class can be invoked without creating a new instance (creating a new object)
“void” specifies a return type, in this case since it is void, this method returns nothing
“main” this is not a keyword, instead the JVM (Java Virtual Machine) looks for this as the start of the program
“String[] args” this is the argument or parameter that the method accepts. It is and array of strings called “args”. These are called Java command-line arguments
Data Type: Strings (not really a data type)
Stores a series of characters, words and sentences.
Takes up different amounts of memory depending on the size of the string.
Defined using double quotation marks.
Technically this isn’t a variable type, but it sort of is, so we will discuss it here.
Note that it starts with a capital letter.
Ex: “Eric”, “Shairose Holborn”, “Hello there”, “Computer programming is fun!”, “10432 McLeod Street”, “K3N 4H9”.
Data Type: int
Stores positive or negative numbers without decimal places.
Stores values between -2,147,438,648 and 2,147,483,647.
Takes up 32 bits of memory or 4 bytes.
Ex: 45, -234, 0, 65323, -3454
Data Type: double
Stores positive or negative values with decimal places.
Can store immensely huge numbers.
Can store values accurately up to 15 decimal places.
Takes up 64 bits of memory.
Ex: 12.5, -543.7645, 0, 5465.876, -8722.987999
Data Type: Char
Stores a single character.
Takes up 16 bits of memory.
Is defined using single quotation marks.
Ex: ‘A’, ‘v’, ‘X’, ‘d’, ‘-‘, ‘%’, ‘?’, ‘.’.
Note that chars are defined in between single quotes
Data Type: Boolean
Stores either true or false.
Takes up one single bit of memory.
Ex: true, false… that’s it; there are only those two possible values.
Why are Strings not a primitive data type?
Strings have many methods that are used to manipulate them, thus they were made into a class and not into a primitive data type. → Strings can have a value of null. → Primitive types start with lowercase while non-primitive types start with an uppercase → Primitive types have different sizes while non-primitive types have the same size
Data Type: Float
Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
Uses 4 bytes
Declaring Variables
To declare a variable you need a type followed by a name. For example: | double number; |.
→ Make sure to end with semi-colon
→ You can assign a value later | number = 10; | or when you declare the variable you can assign the value | double number = 10; |
Why is Java considered WORA
Java uses something called a JVM (Java Virtual Machine: interpreter) along with a compiler to run Java programs on any platform.
Escape Characters
The backslash character (“") is also known has the escape character. It is used to declare special characters or new lines. Just put the backslash before the character you need.
Quotations
System.out.println(“ " This is how to print quotations " “);
Backslash
System.out.println(“ \”); → This is how to print backslash
Newline (“\n”)
System.out.println(“ This is how to create a new line after a string \n”);
Variables
Memory location in RAM, depending on the type there are different amounts of space taken up. Remember that you can change the values of these variables throughout your program.
Data Type: Short
The short data type is a 16-bit signed two’s complement integer. It has a minimum value of -32,768 and a maximum value of 32,767.
Data Type: Long
The long data type is a 64-bit two’s complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1.
This is a class not a primitive type
Arithmetic Division (A little feature)
If I divide
10 / 4 = 2 ← Java has no regard for the remainder or decimal point so it will just tell how many times the number goes into the other
However
10 / 4.0 = 2.5 ← Java now cares about the variable as the second value is a double, so the answer will be a double
Casting in Java
In Java there are two types of casting: wide and narrow
Widening Casting (Converting a smaller type to a larger type. This is done automatically) byte → short → char → int → long → float → double
Narrowing Casting (Converting larger type to smaller type. This has to be down manually) double → float → long → int → char → short → byte → Java might cut off data. Like when converting from int to byte, Java will by default round down.
Objects and Classes
Classes are usually a template for objects. The object can contain methods and attributes which can be accessed with dot syntax after the class is instantiated.
Mr. Jeg’s Naming Convention
→ Use Camel Case
→ int are declared by “int” at the start of a variable
→ double are declared by “dbl” at the start of a variable
Constant in Java
Declared with final preceding the definition
→ use all uppercase to denote that it is a constant.
final int BOB = 2;
Access Modifiers: Private
The most restrictive modifier. It limits access to methods and variables to the class in which they are declared. “private” is chosen when there is no need to use certain methods or variables outside the class
Access Modifiers: Default
Allows access only from within the current package. If there is no specified access modifier, the method or variable will take on this one. Learn more about the default modifier.
Access Modifier: Protected
Allows access to a method or variable only from within the current package, unless it is accessed through a child class outside of the package. Learn more about the protected modifier.
Access Modifier: Public
The least restrictive modifier. It allows access to a class, method or variable not only from within the class in which it is declared, but outside as well.
Method Overloading
Method overloading is similar to overriding in that it involves methods with the same name. However with overloading, a single Java class can have multiple methods with the same name if they have different parameter lists. Overloaded methods are distinguished by their number and type of parameters.
Constructors
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.
In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling the constructor, memory for the object is allocated in the memory. It is a special type of method which is used to initialize the object. Every time an object is created using the new() keyword, at least one constructor is called.
Example ↓
class Geek { // data members of the class. String name; int id;
// Constructor would initialize data members // With the values of passed arguments while // Object of that class created Geek(String name, int id) { this.name = name; this.id = id; } }
Constructor Overloading
Constructor overloading is a type of method overloading in which there are multiple constructors in a class. This gives the option to instantiate an object with different sets of arguments.