Components of Java

There are a number of different components to Java: 1. Development environment The Java 2 SDK contains the tools and executable code needed to compile and test Java programs. However, unlike a normal language, the Java 2 SDK includes object frameworks for creating graphical user interfaces, for networking and for complex I/O. Normally, in other … Read more

A Closer Look at the First Sample Program

Although Example.java is quite short, it includes several key features that are common to  all Java programs. Let’s closely examine each part of the program. The program begins with the following lines: /* This is a simple Java program. Call this file “Example.java”. */ This is a comment. Like most other programming languages, Java lets … Read more

Compiling the Program

To compile the Example program, execute the compiler, javac, specifying the name of the source file on the command line, as shown here: C:\>javac Example.java The javac compiler creates a file called Example.class that contains the bytecode version of the program. As discussed earlier, the Java bytecode is the intermediate representation of your program that … Read more

Object-Oriented Programming

Object-oriented programming (OOP) is at the core of Java. In fact, all Java programs are to at least some extent object-oriented. OOP is so integral to Java that it is best to understand its basic principles before you begin writing even simple Java programs. Therefore, this topic begins with a discussion of the theoretical aspects … Read more

Java Program to print Odd numbers from 1 to n or 1 to 100

public class JavaEx { public static void main(String args[]) { int n = 100; System.out.print(“Odd Numbers from 1 to “+n+” are: “); for (int i = 1; i <= n; i++) { if (i % 2 != 0) { System.out.print(i + ” “); } } } }

Java Program to Check Armstrong Number

public class JavaEx { public static void main(String[] args) { int num = 370, number, temp, total = 0; number = num; while (number != 0) { temp = number % 10; total = total + temp*temp*temp; number /= 10; } if(total == num) System.out.println(num + ” is an Armstrong number”); else System.out.println(num + ” … Read more

Java Program to Add Two Complex Numbers

  public class ComplexNumber{ double real, img; ComplexNumber(double r, double i){ this.real = r; this.img = i; } public static ComplexNumber sum(ComplexNumber c1, ComplexNumber c2) { ComplexNumber temp = new ComplexNumber(0, 0); temp.real = c1.real + c2.real; temp.img = c1.img + c2.img; return temp; } public static void main(String args[]) { ComplexNumber c1 = new … Read more