Tech Terms
// casting in the case of division
public class division{
public static void main(String args[]){
int a = 2;
int b = 3;
System.out.println(b/a);
}
}
division.main(null);
public class casting{
public static void main(String args[]){
int a = 2;
double b = a;
double c = 2.99;
int d = (int)c;
System.out.println("Widening");
System.out.println(a);
System.out.println(b);
System.out.println("------");
System.out.println("Narrowing");
System.out.println(c);
System.out.println(d);
}
}
casting.main(null);
ArrayList<int> myNumbers = new ArrayList<int>(); // it cannot store integers
ArrayList<Integer> myNumbers = new ArrayList<Integer>(); // stores the object as an integer wrapper class
public class concat{
public static void main(String args[]){
String text1 = "ora";
String text2 = "nge";
String fruit1 = text1 + text2;
String fruit2 = text1.concat(text2);
System.out.println(fruit1);
System.out.println(fruit2);
}
}
concat.main(null);
public class random{
public static void main(String args[]){
double random = Math.random();
System.out.println(random);
}
}
random.main(null);
public class compoundBooleans {
public static void main(String args[]){
int a = 1;
if (a < 10 && a > 0){
System.out.println("a is between 0 and 10");
}
else {
System.out.println("a is either negative or greater than 10");
}
}
}
compoundBooleans.main(null);
public class comparingNumbers {
public static void main(String args[]){
int a = 3;
int b = 3;
System.out.println(Integer.compare(a, b));
System.out.println(a == b);
}
}
comparingNumbers.main(null);
Comparing Strings
There are many ways to compare strings. Some of the common ways include using the String.equals() method
Here is a link to other possible methods
public class comparingStrings {
public static void main(String args[]){
String fruit = new String("Banana");
String fruit2 = new String("Banana");
System.out.println(fruit.equals(fruit2));
}
}
comparingStrings.main(null);
Comparing Objects
Similarly to numbers and strings, you can use the == operators or the equals() method.
== will see if the objects have the same identity, while .equals will see if the objects hold the same value
It is also common to use the assert method which must be imported. Here are outside examples
public class comparingObjects {
public static void main(String args[]){
Integer a = new Integer(1);
Integer b = new Integer(1);
if(a.equals(b)){
System.out.println("true");
}
else{
System.out.println("false");
}
}
}
comparingObjects.main(null);
import java.io.*;
import java.util.*;
public class forLoops{
public static void main(String args[]) {
//for loop
for (int i = 0; i < 6; i+=2 ){
System.out.print("Code ");
}
//enhanced for loop
int[] num = { 1, 2, 3};
for (int a : num) {
System.out.print(a);
}
}
}
forLoops.main(null);
public class whileLoop{
public static void main(String args[]) {
//while loop
int a = 3;
while ( a > 0 ){
System.out.print("code ");
a--;
}
//while do loop
int b = 3;
do {
System.out.println(b);
a++;
} while (b > 7);
}
}
whileLoop.main(null);
public class nestedLoops {
public static void main(String[] args) {
int weeks = 2;
int days = 7;
// outer loop prints weeks
for (int i = 1; i <= weeks; ++i) {
System.out.println("Week: " + i);
// inner loop prints days
for (int j = 1; j <= days; ++j) {
System.out.println(" Day: " + j);
}
}
}
}
nestedLoops.main(null);
public class Class {
int x = 5;
public static void main(String[] args) {
Class obj = new Class();
System.out.println(obj.x);
}
}
Class.main(null);
// Create a Main class
public class Main {
int x; // Create a class attribute
// Create a class constructor for the Main class
public Main() {
x = 5; // Set the initial value for the class attribute x
}
public static void main(String[] args) {
Main myObj = new Main(); // Create an object of class Main (This will call the constructor)
System.out.println(myObj.x); // Print the value of x
}
}
// Outputs 5
Main.main(null);
public class accessor{
private int num;
//An accessor method
public int getBalance(){
return this.num;
}
}
public class mutator{
private int num;
//A mutator method
public void setNum(int newNum){
this.num = newNum;
}
}
// static method
public static int getTotal(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int x = 3;
int y = 2;
System.out.println(getTotal(x,y)); // Prints: 5
}
public class Keyword{
public void exampleMethodOne(){
System.out.println("Hello");
}
public void exampleMethodTwo(){
//Calling a method using this.
this.exampleMethodOne();
System.out.println("There");
}
}