Posts

Showing posts with the label Learn Java

Primitive Data Types in Java with Size and Default Values

Primitive Data Types in Java Primitive data types are basic data types in Java that determine the type and amount of memory required to store a value. Java provides a fixed set of primitive data types for efficient memory usage. There are two main categories of primitive data types: Numeric Non-Numeric 1. Numeric Data Types Numeric data types are used to store numerical values. They are further divided into two types: a) Integer Data Types Integer data types store whole numbers (without decimal points). byte- 8 bits short- 16 bits int- 32 bits long- 64 bits b) Floating-Point Data Types Floating-point data types store decimal numbers. float- 32 bits double- 64 bits 2. Non-Numeric Data Types Non-numeric data types are used to store non-numerical values. char – Stores a single character and occupies 16 bits of memory boolean – Stores true or false (Memory size is JVM-dependent) Default Values in Java A default value is a value automatically assigned by the compiler when a data type is d...

Java Basics: Main Method and println() Method

Main Method in Java The main method is the entry point of a Java program. During program execution, the JVM (Java Virtual Machine) searches for the main method to start the execution. The JVM uses an inbuilt mechanism to locate the main method, which must be written in lowercase exactly as main. Java is case-sensitive, so any change in the method name will cause a runtime error. If the main method is present, the JVM: Locates the bytecode of the main method Converts the bytecode into machine-specific code Passes the machine code to the operating system Produces the output of the Java program If the main method is not found, the JVM generates a runtime error. Syntax of Main Method: public static void main(String[] args) {     // program execution starts here } println Method in Java The println() method is used to display data on the console output. Key points: It prints the given data on the console After printing, it moves the cursor to the next line The println() method bel...

How to Create a Java Class: Step-by-Step Guide

Creation of Java Class A Java class is a blueprint or template used to create Java objects. It defines the properties (variables) and behaviors (methods) that the objects of the class will have. The content defined inside a Java class is represented inside the Java object created from that class. How to Create a Java Class A Java class is created using the class keyword, followed by a valid identifier (class name). Java Class Syntax: class ClassName {     // variables     // methods     // constructors } Important Points About Java Class A Java class can contain: Variables Methods Constructors Blocks and nested classes A Java class must be saved with the .java file extension. The file name should match the class name when the class is declared as public. Main Method in Java For execution, a Java program must contain the main method. The main method acts as the entry point of a Java program. If the main method is not present in the class, the program will c...

Java Programming Features Every Beginner Should Know

Features of Java Java is a powerful programming language that provides several important features, making it popular for developing secure and reliable applications. 1. Platform Independent Java is called a platform-independent language because a Java program does not depend on a specific operating system. When a Java program is compiled, it generates a .class file. This file contains intermediate bytecode, not machine-specific code. The .class file can run on any operating system as long as the system has JVM (Java Virtual Machine) installed. This concept is known as “Write Once, Run Anywhere.” 2. Java Compiler The Java compiler (javac) is a program that converts the source code written by a developer into bytecode. The compiler: Takes a .java file as input Converts it into intermediate machine code Stores the output in a .class file This bytecode is platform independent and can be executed on any system with JVM support. 3. Java Virtual Machine (JVM) The JVM (Java Virtual Machine) ...

Basics of Java Programming: Keywords, Literals, and Identifiers

Basics of Java Program The basics of a Java program include keywords, literals, and identifiers. These are the fundamental building blocks used while writing Java code. 1. Keywords Keywords are reserved words in Java that have predefined meanings and functionality for the Java compiler. Key points: Keywords must be written in lowercase. Java has 50 reserved keywords. Out of these, 48 keywords have predefined meaning and functionality. The remaining 2 reserved words (goto and const) are not used and have no functionality. Examples of Java keywords: class static final public void Keywords cannot be used as variable names, method names, or identifiers. 2. Literals Literals are fixed values that are directly stored in a Java program. Java supports five types of literals: Integer Literals: Eg:- 1, 25, 100 Floating-Point Literals: Eg:- 1.5, 2.75 Character Literals: Eg:- 'a', 'Z' String Literals: Eg:- "Java", "Programming" Boolean Literals: Eg:- true, false...