in Person.java file

Encapsulation is a data hiding mechanism. We achieve encapsulation by making our variables private and providing public getters and setters.

Why do we need to encapsulate? If the property has public access, the client code can have direct access and can assign any value. By encapsulating we have one layer where we can control what comes to our property in our setter method. In the above example, we can see how we are restricting negative age in the setAge method. Another example — let’s say we are creating a custom List data structure based on an array. The underlining array…


The final keyword can be used with variables, methods, and classes.

Photo by Maxim Zhgulev on Unsplash

The final keyword is a special specifier that you can put together with variable, method, and class declarations.

  1. Final variable.
  2. Final method.
  3. Final class.

Final variable

Let’s start with the variables. Once we assigned a value for the final variable there is no way we can reassign it.


static variables and methods belong to its class and not to a specific object.

  • object with reference john assigning the value for static variable address
  • object with reference smith to accessing the static variable and getting the value that was assigned by john. This example shows that static members are kind of global for all objects.
  • The correct way of using the static members is by class name.


Access modifiers are used to control access for variables, methods, and sometimes inner classes.

There are 4 access modifiers available:

public — accessible from everywhere. If we mark it as public it will be accessible from everywhere.

protected — accessible within the same package and child classes.

default — if we don't put anything it’s the default access modifier. It’s accessible within the same package only.

private — accessible within the same class only.

Let’s take a look into the examples with methods:


Objects are the main part of object-oriented programming. It’s crucial to know how they work.

When building software we move and manipulate with a lot of data. Data comes in different forms so we need a way to represent our data in our code. Java provides 8 core primitive data types.

byte       for whole numbers
short for whole numbers
int for whole numbers
long for whole numbers
float for floating numbers
double for floating numbers
char for single character(ACII table)
boolean logical - can be true or false

Using these primitives we can represent whole numbers, floating numbers, single characters…


The main idea of loops is to repeat our code.

Photo by Benjamin Lizardo on Unsplash
  • while loops
  • for loop
  • Iterator and for each loop

While loops

Loops allow us to repeat a block of code a specific number of times depending on a condition.


java.lang.String is the most used class in Java. It’s important to understand and able to manipulate strings fluently.

Photo by Markus Spiske on Unsplash
  • String Class
  • String Methods
  • Comparing Strings

String Class

The String is a built-in class in Java to store a sequence of characters between double-quotes. It’s under java.lang package so we don’t have to import, it will be imported automatically for every class.

public class Main {
public static void main(String[] args) {
String str = "apple";
System.out.println(str); // apple

str = "banana";
System.out.println(str); // banana

String name = new String("John");
System.out.println(name); // John
}
}
  • We can initialize string directly using = operator or…


Enum is a special class that represents constants. It provides type-safe checking. For example, we could create a method that will accept only specific values and for other values, it will give a compilation error. Without enum, we would find out about the wrong value during the runtime only.

  • Enum syntax
  • Enum with methods and switch statement
  • Advance enum

Enum syntax

public enum DayOfWeek {
MON, TUE, WED, THUR, FRI, SAT, SUN
}
  • Enum is a special class for constants so to create the enum we need to write enum instead of class.
  • In the body of enum, we can just list…


output is small number

The ternary operator is a simple conditional branching statement. It returns a value based on condition.

int num = 54;
String str = num > 100 ? "big number" : "small number";

num > 100 ? is our condition.

"big number" the first value(before :) will be returned if a condition is true.

"small number" the second value(after :) will be returned if a condition is false. In our case, this value is returned because the condition is false.

Turnery operator can be used with any other data types as well.

public class Main { public static void main(String[] args)…


If provided value to our switch will match with the value of one of the cases then this case will be executed.

public class Main {
public static void main(String[] args) {
String str = "white";

switch(str) {
case "black":
System.out.println("#000000");
break;
case "white":
System.out.println("#FFFFFF");
break;
case "silver":
System.out.println("#C0C0C0");
break;
default:
System.out.println("default color");
System.out.println("#000000");
}
}
}

The above program will print #FFFFFF because the value of String str is white and the second case has value white so they are matching and this case will get executed. …

Beknazar

Software Developer, Java Instructor https://www.techleadacademy.io/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store