Skip to main content

Posts

Showing posts from March, 2020

how to use java constructor

A constructor is a special method in Java that gets called whenever you create a new object from a class — its whole job is to set that object up and ready to go. Every Java class needs a constructor, but you don't always have to write one yourself. If you skip it, the JVM quietly creates a default one behind the scenes. A default constructor is simply one that doesn't take any parameters — it's there just to let you create the object, without necessarily setting any specific values right away. Here's what that looks like in practice: public class Person { private int age; private String name; private String city; // default constructor public Person() { } // constructor with parameters public Person(int age, String name, String city) { this.age = age; this.name = name; this.city = city; } } With a class like this, you've got options. You can create an object using the default constructor and fill...

Primitive data types

In this section, you'll learn about Java's eight primitive data types — what they are, when to use each one, and why picking the right type actually matters. Think of these as the basic building blocks of any Java program, the same way flour, eggs, and sugar are the basic ingredients in a recipe. Java gives you eight primitive types, covering numbers, characters, and true/false values: byte short int long float double char boolean Let's go through each one. 1. byte: Small but Mighty The byte type holds a narrow range of values — from -128 to 127. Picture it as a small cup, just enough for a quick sip. You'll typically reach for it when memory matters, especially in large arrays where every bit adds up. byte smallNumber = 100; 2. short: A Step Up The short type stores a wider range, from -32,768 to 32,767. Think of it as a medium-sized cup — bigger than a byte, but still more compact than what you'd normally use. short mediumNumber = 10000; 3. int: The Eve...

Using printf to display text into the console

In this section, you'll learn how to use printf to control exactly how your output looks — decimal places, spacing, alignment, and more. By the end, you'll be able to format strings, numbers, and percentages like a pro. What is printf? printf stands for "print formatted." Unlike a regular print statement that just dumps text and numbers as-is, printf gives you control over how that data is displayed. Think of it as the difference between a plain T-shirt and a tailored outfit — same content, much better presentation. The Basic Syntax Every printf statement follows this structure: System.out.printf("format-string", arguments); format-string — defines how the output should look, combining plain text with placeholders for your data arguments — the actual values you want inserted into those placeholders Let's put this into practice with a few examples. Example 1: Formatting a String Start with something simple — printing a name inside a sentence: Str...

Introduction to java

Ever stopped to think about what's actually happening under the hood of your favorite apps? There's a decent chance Java is involved somewhere. It's been called one of the most popular programming languages for a reason — Java has quietly powered a huge chunk of the digital world for decades. From websites to embedded systems tucked inside everyday devices, it's everywhere, even when you don't notice it. So what's the secret? Let's break it down. So, What Exactly Is Java? At its core, Java is a high-level, object-oriented language that Sun Microsystems built back in the mid-90s. The whole idea behind it was refreshingly simple: write your code once, and let it run anywhere — no matter the device. That's possible because of the Java Virtual Machine (JVM), which acts as a translator between your code and whatever hardware it lands on. It sounds almost too convenient, but that's exactly why developers adopted it so fast. A Little Bit of History Java did...