0% found this document useful (0 votes)
13 views

Java Basic 1

it describes the basic nature of java

Uploaded by

ishmamrcs
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Java Basic 1

it describes the basic nature of java

Uploaded by

ishmamrcs
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Java Basic

What is Java?
Java is a popular programming language, created in 1995.

It is owned by Oracle, and more than 3 billion devices run Java.

It is used for:

• Mobile applications (Android apps)


• Desktop applications
• Web applications
• Web servers and application servers
• Games
• Database connection
• And much, much more!

Why Use Java?


• Java works on different platforms (Windows, Mac, Linux, Raspberry Pi,
etc.)
• It is one of the most popular programming languages in the world
• It is easy to learn and simple to use
• It is open-source and free (Non- commercial)
• It is secure, fast and powerful
• It has a huge community support (tens of millions of developers)
• Java is an object-oriented language which gives a clear structure to
programs and allows code to be reused, lowering development costs
• As Java is close to C++ and C#, it makes it easy for programmers to
switch to Java or vice versa

Java Syntax
In the previous chapter, we created a Java file called MyClass.java, and we used
the following code to print "Hello World" to the screen:

public class MyClass {


public static void main(String[] args) {
System.out.println("Hello World");
}
}

Explaination:
Every line of code that runs in Java must be inside a class. In our example, we
named the class MyClass. A class should always start with an uppercase first
letter.
Remember Note:
➢ Java is case-sensitive: "MyClass" and "myclass" has different meaning.

➢ The name of the java file must match the class name.

➢ The main() method is required and you will see it in every Java program.
➢ Any code inside the main() method will be executed.
➢ that every Java program has a class name which must match the
filename.
➢ every program must contain the main() method.

System.out.println()
System.out.println("Hello World");

Note: The curly braces {} marks the beginning and


the end of a block of code.
Note: Each code statement must end with a
semicolon.
Datatypes Variable in Java
Data types are divided into two groups:

• Primitive data types -


includes byte, short, int, long, float, double, boolean and char.
• Non-primitive data types - such as String, Arrays and Classes (you will
learn more about these in a later chapter)

Primitive Data Types


A primitive data type specifies the size and type of variable values, and it has
no additional methods.

There are eight primitive data types in Java:

Data Size Description


Type

byte 1 byte Stores whole numbers from -128 to 127

short 2 bytes Stores whole numbers from -32,768 to 32,767

int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647

long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to


9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7
decimal digits

double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal


digits

boolean 1 bit Stores true or false values

char 2 bytes Stores a single character/letter or ASCII values


Most Used Java Variables for
android:
Variables are containers for storing data values.

In Java, there are different types of variables, for example:

• String - stores text, such as "Hello". String values are surrounded by


double quotes
• int - stores integers (whole numbers), without decimals, such as 123 or -
123
• float - stores floating point numbers, with decimals, such as 19.99 or -
19.99
• char - stores single characters, such as 'a' or 'B'. Char values are
surrounded by single quotes
• boolean - stores values with two states: true or false
Declaring Variables
To create a variable, you must specify the type and assign it a value:

type variable

int myNum = 5;

float myFloatNum = 5.99f;

char myLetter = 'D';

boolean myBool = true;

String myText = "Hello";

Recommendation :
All Java variables must be identified with unique names.

These unique names are called identifiers.

Identifiers can be short names (like x and y) or more descriptive names (age,
sum, total Volume).

Note: It is recommended to use descriptive names in order to create


understandable and maintainable code:

The general rules for constructing names for variables (unique


identifiers) are:

• Names can contain letters, digits, underscores, and dollar signs


• Names must begin with a letter
• Names should start with a lowercase letter and it cannot contain
whitespace
• Names can also begin with $ and _ (but we will not use it in this tutorial)
• Names are case sensitive ("myVar" and "myvar" are different variables)
• Reserved words (like Java keywords, such as int or boolean) cannot be
used as names

You might also like