Inside Java: Made Easy- Part 1

Rezaul H Reza
intro-to-java-made-easy
5 min readAug 12, 2020

Introduction

Java is an object oriented programming language that is class-based, and designed to have as few implementation dependencies as possible. It is intended to let application/software developers write once, run anywhere, meaning that compiled Java code can run on all platforms that support Java without the need for recompilation. Java applications are typically compiled to Java bytecode that can run on any Java Virtual Machine(JVM) regardless of the underlying computer architecture.

Parts of a java program

Class Header — The class header tells the compiler things about the class such as what other classes can use it (public) and that it is a Java class (class), and the name of that class (Simple).

Curly Braces — When associated with the class header, they define the scope of the class. When associated with a method, they define the scope of the method.

Variables and Literals

A variable is a name for a location in memory.

A variable must be declared by specifying its name and the type of information that it will hold.

This line is called a variable declaration.

int value;

The following line is known as an assignment statement.

value = 5; value 5 is stored in memory.

System.out.print(“The value is “); “…..” is string literal.

System.out.println(“Hello “ + “World”); This is + operator with string literals

System.out.println(“The value is: “ + value);

Identifiers

Identifiers are programmer-defined names for:

– classes

– variables

– methods

Variable Names

-Variable names should be descriptive.

-Descriptive names allow the code to be more readable; therefore, the code is more maintainable.

Which of the following is more descriptive?

double tr = 0.0725; ❌

double salesTaxRate = 0.0725; ✔️

Why is that so?

Jobs- Study, in brief, for your own benefits, code readability is really important.

Java Naming Conventions

Variable names should begin with a lower-case, then shift to title case.

Ex : int salesTaxRate;

Class names should be : title case.

Ex: public class LovingJava

A general rule of thumb about naming variables and classes are that, with some exceptions, their names tend to be nouns or noun phrases.

Primitive Data Types

There are eight primitive data types in Java .

4 integers: byte, short, int, long

2 floating point numbers: float, double

1 characters : char

1 boolean: boolean(True/False)

Variable Assignment and Initialization

In order to store a value in a variable, an assignment statement must be used. The assignment operator is the equal (=) sign(stated above).The operand on the left side of the assignment operator must be a variable name. The operand on the right side must be either a literal or expression that evaluates to a type that is compatible with the type of the variable.

Variable Assignment and Initialization

// This program shows variable assignment.

public class Initialize {

public static void main(String[] args) {

int month, days;

month = 1;

days = 14; System.out.println(“Month “ + month + “ has “ + days + “ Days.”); } }

The variables must be declared before they can be used.Once declared, they can then receive a value (initialization); however the value must be compatible with the variable’s declared type. After receiving a value, the variables can then be used in output statements or in other calculations. Local variables can be declared and initialized on the same line( int month-= 1. days= 14;).

Things to remember:

Variables can only hold one value at a time.

Local variables do not receive a default value.

Local variables must have a valid type in order to be used.

Integer Data Types

byte, short, int, and long are all integer data types.

They can hold whole numbers such as 5, 10, 23, 89, etc.

Integer data types cannot hold numbers that have a decimal point in them.

Integers embedded into Java source code are called integer literals.

Floating Point Data Types

Data types that allow fractional values are called floating-point numbers. — 1.7 and -45.316 are floating-point numbers.

In Java there are two data types that can represent floating-point numbers.

– float — also called single precision (7 decimal points).

– double — also called double precision (15 decimal points.)

Floating Point Literals

When floating point numbers are embedded into Java source code they are called floating point literals.

The default type for floating point literals is double. — 29.75, 1.76, and 31.51 are double data types.

A double value is not compatible with a float variable because of its size and precision.

– float number; — number = 23.5; // Error!

A double can be forced into a float by appending the letter F or f to the literal.

– float number; — number = 23.5F; // This will work

The boolean Data Type

. The Java boolean data type can have two possible values.

– true

– false

The value of a boolean variable may only be copied into a boolean variable.

The char Data Type

The Java char data type provides access to single characters.

char literals are enclosed in single quote marks.

– ‘a’, ‘Z’, ‘\n’, ‘1’

Don’t confuse char literals with string literals. — char literals are enclosed in single quotes. — String literals are enclosed in double quotes.

Arithmetic Operators

The operators are called binary operators because they must have two operands.

Each operator must have a left and right operand.

The arithmetic operators work as one would expect.

It is an error to try to divide any number by zero.

When working with two integer operands, the division operator requires special attention.

Coming More….

--

--

Rezaul H Reza
intro-to-java-made-easy
0 Followers

Full Stack Mid Level Developer at Insight Technology Ltd, United Kingdom