You are on page 1of 8

CHAPTER 1.

INTRODUCTION TO JAVA PROGRAMMING


Java is a popular programming language, created in 1995.

It is used for:

• Mobile applications (specially Android apps)


• Desktop applications
• Web applications
• Games etc.

In TERM I, we have learnt C++ programming.

Now let us learn few basics of Java Programming.

Let’s create a basic Java program. Through this program, we will be able to
understand the difference in the syntax between C++ and Java.

A Java Program to print "Hello World" on the Output screen:

class MyProgram
{
void main()
{
System.out.println("Hello World");
}
}
The above program explained:

Every line of the program in Java must be inside a class. In our example, we named the
class as MyProgram.

The main() method is required and you will see it in every Java program.
Inside the main() method, we can use the System.out.println() to print a line of text to
the output screen.
Most of the concepts of variables, data types and operators remain same in Java.
So, in this chapter, we will convert all our programs done in C++ to Java.
We will use BlueJ, which is an integrated development environment (IDE) for
the Java programming language, developed mainly for educational purposes, but also
suitable for small-scale software development. We will write our Java programs in BlueJ.

The following image shows a BlueJ window with the class MyProgram.

1
***********

2
CHAPTER 2: Data types used in Java
In Java Programming we have to deal with the various types of data types, hence
becomes necessary for the programmer to select appropriate data type according to the
data taken in the program.
Data types are divided into two categories
• Primitive Data type (Basic data/inbuilt data type)
• Non-Primitive Data type (Composite/ Derived Data type)
Different primitive and non-primitive data type data types used in Java are
Name Data type Size Default value
Boolean boolean 1 bit false
Byte byte 1 byte 0
Character char 2 bytes ‘\u0000’
Short short 2 bytes 0
Integer int 4 bytes 0
Long long 8 bytes 0L
Float float 4 bytes 0.0f/0.0F
Double double 8 bytes 0.0d/0.0D
String String No of characters null/ “ ”
* 2 bytes

Difference between primitive and non-primitive data types


Primitive data type Non-Primitive data type
Independent of any other data type Dependent on Primitive data type
They are inbuilt in java library They are derived from basic data type
What is expression in Java
An expression is a series of variables, operators, and method calls (constructed according to
the syntax of the language) that evaluates to a single value.
They are of two types
Pure expression: Expression with one data type is called as pure expression
int a=5;

int b=10;

System.out.println(a+b);

Output : 15;

Mixed expression : Expression with more than one data type is called as mixed expression
int a=5;

float b=10.0f;

System.out.println(a+b);

Output : 15.0;

3
Comment Lines: They are known to be Non executable statements in Java used to put
comments in your coded program for programmer reference. They are of 3 types
• // Single line Statement: The single-line comment is used to comment only one line of
the code. Any text in front of // is not executed by Java.

Example

public class Program1


{
public static void main()
{
int i=10; // i is a variable with value 10
System.out.println(i); //printing the variable i
}
}

• /* */ Multiline Statement: The multi-line comment is used to comment multiple


lines of code. It can be used to explain a complex code snippet or to comment multiple
lines of code at a time (as it will be difficult to use single-line comments there).
Example
public class Program2 {
static void main()
{
/* Let's declare and
print variable in java. */
int i=10;
System.out.println(i);
/* float j = 5.9;
float k = 4.4;
System.out.println( j + k ); */
}
}
• /** */ Document line comment: Documentation comments are usually used to write
large programs for a project or software application as it helps to create documentation

Why do we use comments in a code?


Comments are used to make the program more readable by adding the details of the code.

It makes easy to maintain the code and to find the errors easily.

The comments can be used to provide information or explanation about the variable,
method, class, or any statement.

It can also be used to prevent the execution of program code while testing the alternative code.

4
CHAPTER 3:
Inputs in Java
This chapter we will learn various was to input data values in the program.
They are of three types.
1. By using Assignment statements.
2. By using Function Arguments.
3. By using Scanner class (will study in std 9)

1. Assignment statements: A statement which stores values in a variable is called


assignment statemen. We can write program using assigning values to the variable.
Example
Write a program to find the area of rectangle whose length is 3.5 cm and breadth is 5 cm.
public class area
{
static void main()
{
float l=3.5f; //length of rectangle
int b=5; // breadth of rectangle
float a;
a=l*b; // to calculate area of rectangle
System.out.println("Area of rectangle is = " + a);
}
}

2. Function Arguments: This is one of the methods to accept the value from the user at
the time of execution of the program. The values are to be input in method call window
must be provided as arguments in the main( ) function.

Example
Write a program to find the area of rectangle whose length is 3.5 cm and breadth is 5 cm.
public class area
{
static void main(float l, int b) //Input length & breadth of rectangle through arguments
{
float a;
a=l*b; // to calculate area of rectangle
System.out.println("Area of rectangle is = " + a);
}
}
Exercise
Convert c++ programs to Java programs

5
CHAPTER 4 : IF – ELSE - IF STATEMENT WITH LOGICAL
OPERATORS IN JAVA
Example 1
“If you have money and if your mother gives you permission then you can go and have
an ice cream”.
In the above statement, we understand that unless both the conditions are satisfied,
you cannot eat ice cream.

Example 2
If x is greater than y and x is greater than z, then x is the largest number amongst the
three numbers x, y and z.
In the above statement, we compare x with the other two numbers y and z and then
arrive at a conclusion that x is the largest.
In order to solve the above program, we require Logical operators, which help us to
combine two or more conditions together.

Logical operator Symbol Syntax

and && x > y && x > z

or || a= =b || b= =c

Logical operator and (&&)


This operator is used to compare and check if all the given conditions are true. If yes,
then action to be taken is performed.
Logical operator or ( | | )
This operator is used to compare and check any one of the given conditions is true. If
yes, then action to be taken is performed.

Program
public class hotel
{
static void main(int age,int money)
{
if(age>=15 && money>=300)
{
System.out.println("You an eat outside");
}
else
{
System.out.println("You cannot eat outside");
6
}
}

Output of the program


If you enter 13 as age and 350 as amount in method call window then the output is

sorry, you have to eat at home

In the above program, only one condition is satisfied or true. Hence, the above output.

Exercise:
1.Write a program to enter a character and check if it is a vowel or a consonant.
2.Write a program to input a number and find out if it is single digit or a double
digit number. Display the output with proper messages.
3. Write a program to enter a character and check whether it is a capital letter or a small
letter.

IF -ELSE-IF STATEMENT
When you have to check for multiple conditions, then we use the if else-if statement in
C++.
Syntax
if (condition 1)
{
action to be taken if condition 1 is true;
}
else if (condition 2)
{
action to be taken if condition 2 is true;
}
else
{
action to be taken if both conditions 1 and 2 are false;
}
Multiple else –if statements is also called as IF -ELSE-IF LADDER.
Program To find the greatest among 3 numbers.
public class greatest
{
static void main(int a,int b,in c)
{

if(a> b && a>c)


System.out.println(a+" is greatest");
else if(b>a && b>c)
System.out.println(b+" is greatest");
else
System.out.println(c+" is greatest");
}
}

Exercise:
1. Write a program to accept three numbers and find the largest among them.
2. Write a program to accept three sides of a triangle and check if the triangle is
equilateral, isosceles or scalene.
3. Write a program to input a number and find out if it is single digit, double digit,
7
three digit or more than three digits. Display the output with proper messages.
4. Write a program in C++ to accept a number and check :
(a) whether it is divisible by 3 and 7.
(b) whether it is divisible by 3 but not by 7.
(c) whether it is divisible by 7 but not by 3.

You might also like