Core Java Flashcards
Primitive Types
A primitive data type specifies the size and type of variable values, and it has no additional methods.
final
If you don’t want others (or yourself) to overwrite existing values, use the final keyword final int myNum = 15;
types
int myNum = 5; // Integer (whole number) float myFloatNum = 5.99f; // Floating point number char myLetter = ‘D’; // Character boolean myBool = true; // Boolean String myText = “Hello”; // String
Casting
In Java, there are two types of casting: Widening Casting (automatically) - converting a smaller type to a larger type size byte -> short -> char -> int -> long -> float -> double Narrowing Casting (manually) - converting a larger type to a smaller size type double -> float -> long -> int -> char -> short -> byte int myInt = (int) myDouble; // Manual casting: double to int
main, sop, comments
public class Main { public static void main(String[] args) { System.out.println(“Hello World”); } } Comments // and /* */
String
Method Description 1. length() Returns the length of String name. (5 in this case) 2. toLowerCase() Converts all the characters of the string to the lower case letters. 3. toUpperCase() Converts all the characters of the string to the upper case letters. 4. trim() Returns a new String after removing all the leading and trailing spaces from the original string. 5. substring(int start) Returns a substring from start to the end. Substring(3) returns “ry”. [Note that indexing starts from 0] 6. substring(int start, int end) Returns a substring from the start index to the end index. The start index is included, and the end is excluded. 7. replace(‘r’, ‘p’) Returns a new string after replacing r with p. Happy is returned in this case. (This method takes char as argument) 8. startsWith(“Ha”) Returns true if the name starts with the string “Ha”. (True in this case) 9. endsWith(“ry”) Returns true if the name ends with the string “ry”. (True in this case) 10. charAt(2) Returns the character at a given index position. (r in this case) 11. indexOf(“s”) Returns the index of the first occurrence of the specified character in the given string. 12. lastIndexOf(“r”) Returns the last index of the specified character from the given string. (3 in this case) 13. equals(“Harry”) Returns true if the given string is equal to “Harry” false otherwise [Case sensitive] 14.equalsIgnoreCase(“harry”) Returns true if two strings are equal, ignoring the case of characters. Escape character \ String Concatenation 1. + character 2. concat method firstName.concat(lastName)
Ternary Operator
String result = (time < 18) ? “Good day.” : “Good evening.”;
Switch
int day = 4; switch (day) { case 1: System.out.println(“Monday”); break; default: System.out.println(“Looking forward to the Weekend”); }
Math
Math.max(5, 10); Math.min(5, 10); Math.sqrt(64); Math.abs(-4.7); Math.random(); //returns a random number between 0.0 (inclusive), and 1.0 (exclusive):
If Else
if (condition1) { // block of code to be executed if condition1 is true } else if (condition2) { // block of code to be executed if the condition1 is false and condition2 is true } else { // block of code to be executed if the condition1 is false and condition2 is false }
while
while (condition) { // code block to be executed }
for , for each
for (int i = 0; i < 5; i++) { System.out.println(i); } “for-each” loop, which is used exclusively to loop through elements in an array String[] cars = {“Volvo”, “BMW”, “Ford”, “Mazda”}; for (String i : cars) { System.out.println(i); }
methods
method must be declared within a class void means no return static void myMethod(String fname, int age) { // code to be executed }
Method overloading
int myMethod(int x) float myMethod(float x) double myMethod(double x, double y)
Classes and Objects
public class Main { int x = 5; } Main myObj = new Main();
Static Methods
can be accessed using class name only
Constructor
Constructor name must match the class name, and it cannot have a return type (like void) constructor is called when the object is created. All classes have constructors by default: if you do not create a class constructor yourself, Java creates one for you. However, then you are not able to set initial values for object attributes.
Access Modifiers
Classes public The class is accessible by any other class default The class is only accessible by classes in the same package. This is used when you don’t specify a modifier. For attributes, methods and constructors public private default - The code is only accessible in the same package. protected - The code is accessible in the same package and subclasses.