JavaRush /Java Blog /Random EN /Coffee break #166. Why is encapsulation needed in Java? T...

Coffee break #166. Why is encapsulation needed in Java? Three Ways to Use the Print Function in Java

Published in the Random EN group

Why is encapsulation needed in Java?

Source: Use My Notes In this post you will learn why encapsulation is needed in object-oriented programming. The article discusses the concept of its operation and methods of implementation in the Java language. Coffee break #166.  Why is encapsulation needed in Java?  Three Ways to Use the Print Function in Java - 1

What is encapsulation in Java

Encapsulation is a concept in the Java language that combines data and the methods that operate on it into a single package or wrapper. Simply put, encapsulation combines variables and methods in one class.

Why do we need encapsulation in Java?

  • To keep your code clean and organized.
  • For better access control to encapsulated members.
  • To better understand the code.
Let's understand this concept with a small example:
class MyBankAccount {
    private int acc_number;
    private int acc_balance;

    public MyBankAccount(int acc_number, int acc_balance) {
        this.acc_number = acc_number;
        this.acc_balance = acc_balance;
    }

    public int printAccountBalance() {
        System.out.println("Balance: " + acc_balance);
    }

    public int printAccountNumber() {
        System.out.println("Account Number: " + acc_number);
    }

    public void depositMoney(int money) {
        acc_balance = acc_balance + money;
    }
}
Here we have a MyBankAccount class with a constructor, two variables and three methods. These class members are related to each other, so they are in the same class. The MyBankAccount class encapsulates or combines the contents of the class, and all of this code acts as a single unit. Now, just by looking at the class name, i.e. “MyBankAccount”, we can assume that it has an account balance and an account number (which it has as variables). Bank accounts are associated with monetary transactions such as depositing money, listing account balances, and so on. These transactions are performed using methods corresponding to the above class. As we can see, this has improved the readability and maintainability of the code. Let's say we have 100 classes and each class has 5 to 10 methods and almost the same number of variables. With encapsulation, it's much easier for us to find specific methods or variables by simply guessing what class they might belong to.

How can the concept of encapsulation help you in your work?

Encapsulation in programming has several advantages, we just don't realize them until we see them in action. In the modern world, everything is digital and software in one way or another. There are many programming languages ​​in which a huge amount of code is generated when developing software or programming libraries. They are all encapsulated in one way or another. Without encapsulation, programming would be a mess (especially in the software industry). Encapsulation in Java:
  • Helps organize code better and more clearly.
  • Allows you to spend less effort when maintaining a large amount of code.
  • Reduces code management complexity.
  • Divides the code into parts.
  • Improves readability.

What if there was no encapsulation?

There are programming languages ​​that do not have the concept of encapsulation. One of them is the C programming language. It has no concept of encapsulation. The code can be scattered across files, and each file can have any variable or function that may or may not be related to each other. This creates confusion in code management and increases complexity. Without encapsulation:
  • The code may become disorganized or cluttered.
  • The complexity of code maintenance increases.
  • Debugging code becomes more difficult.
  • Readability decreases.
I hope you now have a better understanding of encapsulation and its use in Java programming. Thank you for reading.

Three Ways to Use the Print Function in Java

Source: FreeCodeCamp Not all developers know about the three different Print functions/operators in Java. The author of this article will talk about them and show how they work with examples. Coffee break #166.  Why is encapsulation needed in Java?  Three Ways to Use the Print Function in Java - 2

How to use println() function in Java

The println() function adds a new line after printing the value/data inside it. Here the ln suffix works like the newline character \n . Let's look at the sample code:
public class Main{
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}
If it is not yet very clear what exactly is happening, then it can be briefly explained as follows. When you print just one line, this is the output you get:
Hello World!
Now, if you try to print several different expressions using println() , you can clearly see the difference!
public class Main{
    public static void main(String[] args) {
        System.out.println("Hello World!");
        System.out.println("Welcome to freeCodeCamp");
    }
}
As you can see, after the first print statement is executed, one newline character ( \n ) is added. So you get the second print statement Welcome to freeCodeCamp on the next line. The entire output will be as below:
Hello World! Welcome to freeCodeCamp
But isn't there a way to escape the auto-generated newline in the print function ? Eat! In this case, you need to use the print() statement .

How to use print() function in Java

To demonstrate this feature, let me use an example that will help you see the difference right away:
public class Main{
    public static void main(String[] args) {
        System.out.print("Hello World!");
        System.out.print("Welcome to freeCodeCamp");
    }
}
As you can see, I used print instead of using println as before. The print statement does not add an extra \n as a newline character after performing a task within it. That is, you won’t get another new line in the output, but something like this:
Hello World! Welcome to freeCodeCamp
If you want, you can solve the \n problem as below:
public class Main{
    public static void main(String[] args) {
        System.out.print("Hello World!\n");
        System.out.print("Welcome to freeCodeCamp");
    }
}
This time \n will work as a newline character and you will get a second line. Here's the result:
Hello World! Welcome to freeCodeCamp
You can also print two lines using just one print statement , as shown below:
public class Main{
    public static void main(String[] args) {
        System.out.print("Hello World!\nWelcome to freeCodeCamp");
    }
}
The output will be the same:
Hello World! Welcome to freeCodeCamp

How to use printf() function in Java

The printf() function works like a formatted print function . To better understand, here are two scenarios: Scenario 1 . Your friend Tommy wants you to email him a PDF file. You can simply create an email with a subject line of your choice (eg, hi Tommy, this is Jim). You can even leave out the body of the email and send a blank email with just a PDF attachment. Scenario 2 . You couldn't come to class yesterday. Your teacher has asked you to provide reasons for absence with evidence and send documents by email. Here, you can't send a letter to your professor the way you did for your friend Tommy. You need to maintain some formality and proper etiquette. That is, in the letter you must indicate a formal topic and write the necessary information in the body. Last but not least, you should attach the document to your email after renaming it according to the proper naming convention. The point is that you need to format your email in the way that is required of you. The printf() function helps us implement the second scenario. If we want to specify a specific print format/style, we use the printf() function . Here's a quick example of how this works:
public class Main{
    public static void main(String[] args) {
        double value = 2.3897;
        System.out.println(value);
        System.out.printf("%.2f" , value);
    }
}
Here I declare a double variable called value and assign the value 2.3897 to it . Now when I use the println() function it prints the entire value with four digits after the radix point. Here's the result:
2.3897 2.39
After that, when I use the printf() function , I can change the output stream so that the function prints a value. Here I tell the function that I want exactly two digits to be output after the radix point. So the function prints the value rounded to two digits after the radix point. And this is just one way to use the printf() function . Keep in mind that it has many uses in the Java language.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION