Java for Begineers Coding.kowledge PDF
Java for Begineers Coding.kowledge PDF
This is cheat sheet prepared for those who have already knows programming
and wants to learn Java.
This sheets covers basic to intermediate concepts such as getting input from
the user to vectors and strings .
Basically learn java in one day.
- By Smukx
Basic Input/Output
Output: System.out.println()
System.out.println(...) : Prints the argument passed to it and then terminates
the line.
Example:
System.out.println("This is a line.");
System.out.print("This is ");
System.out.print("the same line.");
System.out.printf("Number: %d, String: %s\n", 10, "exampl
e");
Input: Scanner
To read input from the user, you can use the Scanner class.
Example:
import java.util.Scanner;
Common Constants:
Math.PI : The value of π (pi), approximately 3.14159.
Common Methods:
1. Basic Arithmetic:
3. Logarithms:
4. Trigonometric Functions:
Examples
Here are a few examples of how to use these methods:
// Trigonometric Functions
double sinVal = Math.sin(Math.PI / 2); // 1.0
double cosVal = Math.cos(0); // 1.0
double tanVal = Math.tan(Math.PI / 4); // 1.0
// Logarithms
double logVal = Math.log(10); // 2.3025850929940
46
double log10Val = Math.log10(10); // 1.0
value.
: Creates an
Integer(String s) Integer object representing the value of the
specified String .
Example:
2. Constants
Example:
Example:
5. Comparisons
compareTo(Integer anotherInteger) : Compares two Integer objects numerically.
equals(Object obj) : Compares this Integer object to the specified object for
equality.
Example:
Example:
value.
reverse(int i) : Returns the value obtained by reversing the order of the bits
in the two's complement binary representation of the specified int value.
Example:
These features make the Integer class a powerful utility for working with
integer values in Java, offering more functionality and safety compared to
using the primitive int type directly.
also we can able to convert the interger to hex and oct too by using
Integer.toHexString()
toLowerCase()
example
Loops in Java
1. Basic Loops
Example:
Syntax:
while (condition) {
// Code to execute
}
Example:
do {
// Code to execute
} while (condition);
Example:
Example:
Example:
continue : Skips the current iteration and proceeds to the next one.
Strings in Java
1. Basic String Operations
Examples:
split(String regex) : Splits the string into an array based on the given regular
expression.
Examples:
Examples:
Examples:
string that matches the given regular expression with the given
replacement.
Examples:
// StringBuffer (thread-safe)
StringBuffer sbf = new StringBuffer("Hello");
sbf.append(", World!");
System.out.println(sbf.toString()); // "Hello, World!"
matches(String regex) : Tells whether the string matches the given regular
expression.
Example:
😊";
😊
String emoji = "
System.out.println("Emoji: " + emoji); //
Example:
1. Arrays in Java
Example:
Example:
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
System.out.println(matrix[1][1]); // Output: 5
loops.
Example:
import java.util.Arrays;
Dynamic Array Alternatives: Since arrays have a fixed size, dynamic data
structures like ArrayList are often used when the size needs to change
frequently.
2. Vectors in Java
Vectors are part of the java.util package and implement the List interface.
Unlike arrays, vectors are dynamic and can grow or shrink in size. They are
synchronized, making them thread-safe but potentially less efficient than other
list implementations.
Example:
Example:
Common Methods:
Example:
// Convert to Array
String[] array = vector.toArray(new String[0]);
Legacy Nature: Vectors are considered a legacy class. Although they are
still used, modern applications often prefer ArrayList for non-thread-safe
implementations and CopyOnWriteArrayList or other concurrent collections for
thread-safe implementations.