Java is a popular programming language invented by James Gosling in 1995. It is a programming language used to develop mobile apps, web apps, games and much more.
Java is a programming language is object oriented (Class and Object).
how to install Java Development Kit on your PC;
There are open source like java openjdk, adoptium, intelliJ
checking if java is installed on your PC
for compiler:
javac -version
for the language:
java -version
Java file
// public is an access modifier , other type can be default, private, protected
// class - blue print
public class Main{
// void: return nothing
// System.out.println() for printing to our console
public static void main(String[] args){
System.out.println("hello world"); // String must be wrapped in double quote ""
}
}
Extension is .java
flow: javac Main.java will give us Main.class
Java variables
public class Main{
public static void main(String[] args){
Data types
// categories of data types:
// 1. primitives
// 2. reference
// primitives: numbers, fractions, boolean, char
// whole numbers: integers, short, byte, long
// declare a variable: e.g variable name here is myAge and age
// assign a value to the variable
// the = is the assignment operator
// every statement must end with ;
int age = 89;
byte myAge = 127; // byte start -128 to 127 , naming is called camelCase
long myLong = 98765456L;
// float and double: double has higher precision than float, default double
double height = 1.8;
float width = 90.89F;
float my_width = 90.89f; // snake case
float PI = 90.78F; // pascal
To print to console
// sout
System.out.println(age);
Concatenation
// + is an artihmetic operator but can be used to concatenate 2 variables together
System.out.println("my age is " + age);
// char
char grade = 'A';
System.out.println(grade);
// boolean
boolean isTall = false;
System.out.println(isTall);
// String is reference type and inbuilt
String fname = "Ayomide";
String lname = "Fashola";
System.out.println(fname + " " + lname);
// difference between primitives and reference
// primitives - starts with lower while Reference starts with upper
// for every class there are methods
int res = fname.length(); // .name means you are calling a method, a method will have .()
System.out.println(res);
}
}
Java conditionals
public class Main {
// control flow aka conditionals
public static void main(String[] args) {
boolean isRaining = true;
if(isRaining){
System.out.println("go to bed");
}else{
System.out.println("go to school");
}
boolean isMarried = true;
int weddingAge = 6;
boolean willingTomarry = true;
boolean gr = true == true;
System.out.println("is true == to true: " + gr);
System.out.println(true && false);
System.out.println(true && true);
System.out.println(6 < 6);
if(isMarried == true && weddingAge < 6){
System.out.println("not qualified for falentine");
} else if(willingTomarry == true){
System.out.println("you will be considered");
} else{
System.out.println("your matter tire us");
}
}
Java loop
public class Main {
public static void main(String[] args) {
// loops
// int i = 0;
// i++; // i = i + 1; // increment operator
// i++;
// System.out.println(i);
// System.out.println(i < 5);
// DRY - do not repeat yourself
// FOR LOOP
for(int i = 0; i < 5; i++){
System.out.println("ayomide");
}
// while loop and do while loop
}
}
Java Arrays
public class Main {
public static void main(String[] args) {
// Arrays
int age = 90;
// index based/ access elements in arrays
// 0,1,2 ..... n
int[] studentsAge = {1,2,3,4,5,67,90};
System.out.println(studentsAge[2]);
System.out.println("the numbers of elements is " + studentsAge.length);
for(int j = 0; j < studentsAge.length; j++ ){
System.out.println("index is " + j + " and the value is " + studentsAge[j]);
}
for (int elements : studentsAge) {
System.out.println("i am number " + elements);
}
String name = "ronaldo";
String nameToUpper = name.toUpperCase();
System.out.println(nameToUpper); // String methods
}
}
Thank you
Top comments (0)