0% found this document useful (0 votes)
51 views17 pages

Java Programming Basics for ICSE Grade 8

Basic java concepts to be introduced for grade 8 ICSE ICT

Uploaded by

Nilofar Shaikh
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)
51 views17 pages

Java Programming Basics for ICSE Grade 8

Basic java concepts to be introduced for grade 8 ICSE ICT

Uploaded by

Nilofar Shaikh
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

EuroSchool ICSE Grade-8

Programming with Java


Java is a high-level programming language and is very popular
amongst software developers. One reason for Java’s popularity is
that it is platform independent. This means that the same Java
program can be run on many different types of computers.

Java was first released by Sun Microsystems in May 1995 by a


team of software developers headed by James Gosling.

BlueJ is Java Integrated Development Environment specifically


designed for teaching. It is very useful for familiarizing oneself with
the Java syntax and for basic software development.

Java is known as an object-oriented programming(OOP) language.

Just like in English language, to learn the language we start with


‘words’ similarly in Java Language we have ‘tokens’

Tokens
A token is functional and fundamental unit of computer program.
Each program statement is composed of various components
known as a ‘Token’.
Various tokens in Java are:
1. Literals(Constants)
2. Keywords
3. Identifiers(Variables)
4. Operators

1. Literals(Constants)
Literals are the constants i.e. the fixed values in Java program.
Java literals can be classified as:
 Integer Literals e.g. 14, 345, -18, -391
 Real Literals e.g. 24.6, 0.0072, -3.652
 Character Literals e.g. ‘A’, ‘d’, ‘3’, ‘*’
 String Literals e.g. “COMPUTER”, “Year 2020”, “10% per annum”
 Boolean Literals only two possible values– true or false(never
enclosed in quotes)
E.g. boolean ans = true;

Programming with Java 1


EuroSchool ICSE Grade-8

2. Operators
Operators are basically symbols or tokens to perform arithmetic or
logical operations.
Types of operators:
i. Arithmetical operators:
Arithmetic operators are used to perform common mathematical
operations. For example +, -, *, /, % for addition, subtraction,
multiplication, division and modulus respectively.
ii. Logical operators:
Logical operators are used to determine the logic between
variables or values. For example
Operator Name Description Example
&& Logical returns true if both x < 5 && x < 10
And statements are true
|| Logical Or Returns true if one of x < 5 || x < 4
the statements is true
! Logical Not Reverse the result, !(x < 5 && x < 10)
returns false if the
result is true

[Link] operators:
Relational operators are used to compare two values (or
variables). The return value of a relational operator is
either true or false. For example <(less than), <=(less than or
equal to), >(greater than), >=(greater than or equal to),
==(equal to), !=(not equal to)

Programming with Java 2


EuroSchool ICSE Grade-8

3. Identifiers(Variables)

Variable is named memory location that contains a value.


Value can change

Syntax: <data type> <space><variable name>


o e.g int m;
o float p,q,r;

Rules for naming a variable


 Java keywords cannot be used as a variable name.
 A variable name must start with a letter or an underscore
character (_)
 A variable name cannot start with a digit.
 A variable name can only contain alpha-numeric characters and
underscores ( a-z, A-Z , 0-9 , and _ )

Initializing a variable
Uninitialized variable contains garbage value(absurd value). A
variable can be initialized as follows:
Direct assignment of a constant to a defined variable
e.g. int a = 5;
float b = 10.5;
double d = a + b;

4. Keywords
Keywords are reserved words which are preserved by the system
and carry special meaning for the system compiler.
E.g. class, public, etc

Programming with Java 3


EuroSchool ICSE Grade-8

Data types in Java


Data types in Java are of different sizes and values that can be
stored in the variable.
There are 8 primitive data types categorized into 4 different types
as follows:

Integer type
• A variable of integer type can store a whole number.
• Either positive or negative, but without a decimal point.
• It has four types as shown below:

Floating point
• To store a fractional number i.e. a number with decimal points.
• It has two types as shown below:

Programming with Java 4


EuroSchool ICSE Grade-8

Characters
• A character type variable contains a single character.
• Size of character variable is 1 byte i.e. 8 bits
• The declaration of a character can be given as:
• Syntax: <data-type> <variable> = <‘character literal’>;
• For e.g.: char ch = ‘a’;
• A character literal is always enclosed within single quotes.
• Similarly, a set of characters(String) can be declared as,
• Syntax: <data-type> <variable> = <“String constant”>;
• For e.g.: String p = “Computer Applications”;
• A string is always enclosed within double quotes.

Boolean
In programming, when we need a data type that can only have one
of two values, like:
 YES / NO
 ON / OFF
 TRUE / FALSE
For this, Java has a boolean data type having size 1 byte, which
can store true or false values.
For example: boolean ans = true;

Programming with Java 5


EuroSchool ICSE Grade-8

Java Hello World Program


A "Hello, World!" is a simple program that outputs Hello, World! on
the screen.

Programming with Java 6


EuroSchool ICSE Grade-8

Programming Examples:

Example 1:
Write a program to find sum of two values stored in two variables.
public class Example1
{
public static void main()
{
int a=5;
int b=10;
int sum = a + b;
[Link]("Sum is "+sum);
}
}
Output:

Example 2:
Write a program to print a name stored in a string variable.
public class Example2
{
public static void main()
{
String name = "Rahul";
[Link]("Hello "+name);
}
}
Output:

Programming with Java 7


EuroSchool ICSE Grade-8

Example 3:
Write a program to accept a name from main method and print it.
public class Example3
{
//Accepting input through main method
public static void main(String name)
{
[Link]("Hello "+name);
}
}
When executing this program, after selecting void main() we will
get another window where we can enter a value for name.

Output:

Programming with Java 8


EuroSchool ICSE Grade-8

Example 4:
Here’s a program to show the use of all six arithmetic operators
where two integer values are being accepted from the main
method.

public class Example4


{
public static void main(int a, int b)
{
int sum = a+b;
int difference = a-b;
int product = a*b;
int quotient = a/b;
int remainder = a%b;

[Link]("Sum of the two numbers is "


+sum);
[Link]("Difference of the two
numbers is " +difference);
[Link]("Product of the two numbers
is " +product);
[Link]("Quotient after dividing 1st
number by the second number is
"+quotient);
[Link]("Remainder after dividing 1st
number by the second number is
"+remainder);
}
}

Programming with Java 9


EuroSchool ICSE Grade-8

Execution:

Output:

Programming with Java 10


EuroSchool ICSE Grade-8

Example 5:
Let’s see a program to understand the use of post-increment and
post-decrement operators.
public class Example5
{
public static void main(int a, int b)
{
[Link]("Original value of a is "+a);
[Link]("Original value of b is "+b);

a++; //increments the value of the variable by 1


b--; //decrements the value of the variable by 1

[Link]("Changed value of a is "+a);


[Link]("Changed value of b is "+b);
}
}
Execution:

Output:

Programming with Java 11


EuroSchool ICSE Grade-8

Example 6:
Let’s understand relational operators with the help of an example.
As we had seen earlier the return value of a relational operator is
either true or false.
public class Example6
{
public static void main(int a, int b)
{
[Link]("Is (a < b)? "+(a<b));
[Link]("Is (a <= b)? "+(a<=b));

[Link]("Is (a > b)? "+(a>b));


[Link]("Is (a >= b)? "+(a>=b));

[Link]("Is (a == b)? "+(a==b));


[Link]("Is (a != b)? "+(a!=b));
}
}
Output:

Programming with Java 12


EuroSchool ICSE Grade-8

Conditional Construct

Now, we’ll learn how to use the if-else statement in Java.


The if-else statement is the most basic of all control structures,
and it’s likely also the most common decision-making statement in
programming.
It allows us to execute a certain code section only if a specific
condition is met.
Syntax of if…else
The if statement always needs a ‘boolean’ expression as its
parameter.
if (condition)
{
// Executes when condition is true.
}
else
{
// Executes when condition is false.
}
It can be followed by an optional else statement, whose contents
will be executed if the boolean expression is false.

Programming with Java 13


EuroSchool ICSE Grade-8

Example 7:
Let’s understand it with the help of a simple example.
public class Example7
{
public static void main(int a, int b)
{
if(a>b)
{
[Link]("a is greater than b");
}
else
{
[Link]("b is greater than a");
}
}
}
Execution: Output:

Programming with Java 14


EuroSchool ICSE Grade-8

Example 8:
Write a program to check if the given number is odd or even.
public class Example8
{
public static void main(int a)
{
if(a%2==0)
{
[Link]("a is even");
}
else
{
[Link]("a is odd");
}
}
}

Execution: Output:

Programming with Java 15


EuroSchool ICSE Grade-8

Example 9:
Write a program to accept age through main method check if the
person is eligible for voting.
public class Example9
{
public static void main(int age)
{
if(age>=18)
{
[Link]("You're eligible for
voting");
}
else
{
[Link]("You're not eligible for
voting");
}
}
}
Execution:

Output:

Programming with Java 16


EuroSchool ICSE Grade-8

Example 10:
Write a program to accept gender as a character input through
main method and print an appropriate message.
public class Example10
{
public static void main(char gender)
{
if(gender=='M')
{
[Link]("Male");
}
else if(gender=='F')
{
[Link]("Female");
}
}
}
Execution: Output:

Programming with Java 17

Common questions

Powered by AI

In Java, a character is declared using the 'char' data type and is enclosed in single quotes, e.g., char ch = 'a'; . In contrast, a string is declared using the 'String' class and is enclosed in double quotes, e.g., String str = "hello"; . These syntactic differences are significant because they delineate the use-cases: 'char' is for single characters, while 'String' handles sequences of characters. Understanding these differences ensures proper data type selection, facilitates internationalization, and uses Java's rich set of String operations which cannot be applied to char types.

In Java, input values can be handled through the main method by defining parameters within the method's signature. Examples include accepting integer values directly in the method, such as in Example 4 where two integers are passed to calculate arithmetic results . Additionally, character input can be used to execute logic based on value, shown in Example 10 where gender is assessed and the output varies based on the character provided . This approach allows parameterized input handling in command-line applications, facilitating flexible program execution based on provided arguments.

Naming conventions and rules for variables in Java are crucial because they enhance code readability and maintainability. Java variable names must start with a letter or an underscore and can only contain alpha-numeric characters and underscores. They cannot start with a digit or use Java keywords as names . This consistency helps programmers and collaborators easily understand the code's purpose and logic. Well-named variables also prevent errors and facilitate easier debugging by clearly indicating the role and intended use of each variable within a program.

Literals in Java are the constant values that do not change during the execution of a program, while variables are named storage locations that can hold values that may change as a program runs. Literals define fixed values (e.g., 14, "Hello", true). Variables, on the other hand, must be declared with a type and a name and can be initialized with a literal or a computed value . Thus, literals serve as fixed data, while variables help store and manipulate data that varies based on program logic.

Data types in Java, such as integer, floating-point, character, and boolean, define the kind of data variables can hold and restrict operations to compatible values, ensuring type safety. Each data type has a specific memory size and range of values it can represent, which the compiler uses to detect incompatible operations during compile time, reducing runtime errors . Type safety prevents bugs through type checking, and Java's primitive types, by being mapped into efficient memory representations, enhance performance by allowing direct manipulation by the processor without additional overhead.

Arithmetic operators in Java perform mathematical computations such as addition, subtraction, multiplication, division, and modulus (e.g., +, -, *, /, %). These operators are usually used to calculate numeric results. Logical operators, such as && (Logical And), || (Logical Or), and ! (Logical Not), are used to evaluate boolean expressions, combining or negating multiple conditions to produce a true or false result . While arithmetic operators operate directly on numerical data, logical operators manipulate boolean values to control the flow of logic within a program.

Post-increment (a++) and post-decrement (b--) operators in Java increase or decrease the value of a variable by one after the expression in which they occur has been evaluated. For example, using a and b in calculations will utilize their values before the increment or decrement; thus, if a is originally 5, then executing a++ will render a still 5 in the expression but update it to 6 after the expression completes . This functionality is beneficial in loops and conditional constructs where interim logic decisions are based on the current state before the variables are updated for subsequent operations.

The 'if-else' statement in Java is critical for decision-making because it allows the program to execute different code blocks based on given conditions. This structure lets developers implement logic to determine which actions to take at runtime, thereby enabling dynamic behavior within software . For instance, different outputs or operations can be performed depending on evaluations of boolean expressions that assess the current state of variables or input data. This decision-making capability is fundamental to developing complex, responsive applications that can adapt to various user interactions or environmental conditions.

Java's object-oriented programming (OOP) structure emphasizes organizing software design around data, or objects, rather than functions and logic as seen in procedural programming. This approach encourages encapsulation, inheritance, and polymorphism, which means Java programs are built using classes and objects . Each object can contain data, in the form of fields, and code, in the form of methods. This structure allows for code reusability, modularity, and easier management of complex systems compared to procedural languages that focus on sequences of commands. By integrating data and behavior into single units (objects), Java leverages OOP principles to support the design of scalable and maintainable software.

Java's platform independence means that a Java program can run on any device that has a compatible Java Virtual Machine (JVM), regardless of the underlying hardware or operating system. This eliminates the need to rewrite or recompile applications for different platforms, significantly reducing development time and costs. It also ensures consistency in application performance across different environments and simplifies software updates and maintenance . These factors contribute to Java's widespread use in cross-platform applications, particularly in enterprise and web-based systems.

You might also like