Skip to main content

Posts

Showing posts from March, 2020

how to use java constructor

A constructor is a special method in java that is used to instantiate the class when creating object of that class. every java class required to have constructor.If the constructor is not provided then Java JVM will spin a default constructor. A default constructor is constructor that does not takes in any parameters example bellow shows a default constructor. public class Person { private int age; private String name; private String city; //default constructor public Person() { } //class constructor public Person(int age, String name, String city) { this.age = age; this.name = name; this.city = city; } } If we ever wanted to create an object from the class provided above we may supply the arguments or use method called getters and setters to set values of the object.

Primitive data types

In Java, primitive data types are the simplest forms of data. Think of them as the basic ingredients in a recipe. You’ve got types for numbers, characters, and true or false values. There are eight main primitive types in Java: byte short int long float double char boolean Each of these types is unique and serves a different purpose. Understanding the Types 1. Byte: Small but Mighty The byte type can hold a tiny amount of data — from -128 to 127.  It's like a small cup, just the right size for a quick sip. You often use it when you want to save memory, especially in large arrays. byte smallNumber = 100; 2. Short: A Little Bigger Next up is the short . It can store a larger range of values, from -32,768 to 32,767.  Think of short as a medium-sized cup. Great for when you need more than a byte but still want to save some space. short mediumNumber = 10000; 3. Int: The Workhorse The int type is like the standard coffee mug of numbers. It’s widely used for wh...

Using printf to display text into the console

printf stands for “print formatted.” Instead of just printing text and numbers as they are, printf lets you specify how you want them displayed.  This can include setting decimal places, padding numbers, or aligning text. Think of it as giving your output a polished suit instead of just a plain T-shirt. Basic Syntax of printf Before we dive into examples, let’s look at the syntax: System.out.printf("format-string", arguments); format-string : This is where you define how the output looks. You can include text and placeholders for your data. arguments : These are the values you want to display. Now that we know the syntax, let’s check out some practical examples. Example 1: Simple String Formatting Here’s a simple way to print text: String name = "Alice"; System.out.printf("Hello, %s! Welcome to our program.\n", name); In this example, %s is a placeholder for a string. When you run this code, it displays: Hello, Alice! Welcome to our program....

Introduction to java

Ever wonder how your favorite applications come to life?  Java might just hold the key.  Known as one of the most popular programming languages, Java has played a significant role in digital transformation.  From web applications to embedded systems, this language is everywhere.  But what makes Java so special?  Let's dive in and find out. What is Java? Java is a high-level, object-oriented programming language developed by Sun Microsystems in the mid-90s.  Its primary goal was to create a platform-independent language that developers could "write once, run anywhere."  This means Java programs can run on any device with a Java Virtual Machine (JVM). Sounds like magic, right?  It's no wonder Java has been so widely adopted. A Quick History Java was born out of a project called Oak in 1991, headed by James Gosling.  Originally intended for interactive television, it quickly found its footing in the digital world.  Officially launched i...