0% found this document useful (0 votes)
26 views8 pages

Java Programming Basics and Examples

The document provides an introduction to Java programming. It discusses some key differences between Java and C++ syntax, such as how in Java every line of code must be inside a class. It also introduces the main() method and how to print output. The document then discusses Java data types, expressions, and comments. It explains how to accept user input through assignment statements and function arguments. Finally, it covers if-else and if-else-if statements for conditional logic.

Uploaded by

Sonia Sancheti
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views8 pages

Java Programming Basics and Examples

The document provides an introduction to Java programming. It discusses some key differences between Java and C++ syntax, such as how in Java every line of code must be inside a class. It also introduces the main() method and how to print output. The document then discusses Java data types, expressions, and comments. It explains how to accept user input through assignment statements and function arguments. Finally, it covers if-else and if-else-if statements for conditional logic.

Uploaded by

Sonia Sancheti
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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()
{
[Link]("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 [Link]() 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;

[Link](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;

[Link](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
[Link](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;
[Link](i);
/* float j = 5.9;
float k = 4.4;
[Link]( 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
[Link]("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
[Link]("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)
{
[Link]("You an eat outside");
}
else
{
[Link]("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:
[Link] a program to enter a character and check if it is a vowel or a consonant.
[Link] 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)


[Link](a+" is greatest");
else if(b>a && b>c)
[Link](b+" is greatest");
else
[Link](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.

Common questions

Powered by AI

Primitive data types in Java are basic types that are independent and built-in to the language, such as int, float, and boolean. Non-primitive data types, such as String and arrays, are dependent on these basic types and are derived from them .

The main method's signature ('public static void main(String[] args)') defines the program's entry point. 'public' allows access from anywhere, 'static' means it can run without creating an instance of the class, 'void' signifies no return, and 'String[] args' accepts command-line arguments, crucial for standalone execution .

A pure expression in Java involves only one data type, such as 'int a=5; int b=10; System.out.println(a+b);' resulting in 15. A mixed expression involves more than one data type, for example, 'int a=5; float b=10.0f; System.out.println(a+b);' resulting in 15.0 due to type promotion .

Logical operators in Java, such as '&&' (and) and '||' (or), are used to evaluate multiple conditions in decision-making statements like if-else. They enable complex condition-checking, such as allowing actions only when all specified conditions are true with '&&', or if any condition is true with '||' .

The if-else-if ladder allows for the evaluation of multiple conditions sequentially. It improves execution by providing clarity and organization, ensuring only one block of code runs that corresponds with the first true condition, which optimizes decision branching and reduces code complexity .

Comments in Java enhance code readability and maintainability by explaining and documenting code. The types include single-line (//), multi-line (/* */), and documentation comments (/** */) used for generating documentation .

Java requires every line of code to be inside a class, and the entry point of a Java program is the main() method, declared as 'void main()'. In contrast, C++ supports functions outside classes and the main entry point is 'int main()'. Java uses 'System.out.println()' for output, whereas C++ uses 'std::cout' .

Java facilitates beginner programmers through IDEs like BlueJ, which is designed mainly for educational purposes. BlueJ offers a simple user interface, visual methods for understanding object-oriented concepts, and immediate feedback on programming errors, making it suitable for learning Java syntax and logic .

Java differentiates float and double primarily in precision and memory. Float is a single-precision 32-bit type, while double has double precision at 64-bit, supporting more decimal places. This distinction affects memory usage and suitability for applications requiring high precision, like scientific calculations .

Function arguments allow data input at runtime by passing parameters directly into the method, providing flexibility and reusability. Assignment statements, on the other hand, are static inputs coded directly within the program, which limits dynamic user input and often requires code changes for different data .

You might also like