CHAPTER 3

JAVA LANGUAGE BASICS

It is said that the best way to learn swimming is to jump into the water right away. Similarly, the best way to learn a programming language is to start coding straight away. Let us start our study of Java programming in an informal way. Let us write a simple Java program and run it.

3.1 Introduction

In our first Java program, we will find the area of a circle with radius 5.

Problem: Write a program to find area of a circle with radius 5.

Solution: See Program 3.1.

 

PROGRAM 3.1 Area of a Circle

 //    first.java

 class first

  {

 public static void main( String args[] )

  {

   int r ;

   r = 5 ;

   System.out.println(3.14 * r * r);

  }

 }

If you run the program, you will get the following output.

 

Output

 78.5 ...

Get Programming with Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.