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

Java Basic

The document discusses basic Java syntax and concepts like classes, methods, variables, data types, comments, identifiers and type casting. It explains that all Java code must be inside a class and defines the main method as the entry point. It also describes different variable types and how to declare and name variables following Java conventions.

Uploaded by

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

Java Basic

The document discusses basic Java syntax and concepts like classes, methods, variables, data types, comments, identifiers and type casting. It explains that all Java code must be inside a class and defines the main method as the entry point. It also describes different variable types and how to declare and name variables following Java conventions.

Uploaded by

Dipendra Km
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

JAVA BASIC

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

Every Syntax or code must be inside the Class, Here we create the class as name “MyClass” as the Naming
convention the class name must start with Upper letter.
Note: Java is case sensitive program i.e MyClass and myclass are different. The name of the java file must
match the class name. When saving the file, save it using the class name and add ".java" to the end of the
filename.
Main() method
method is the entry point of the program from where the program start to run or execute.
System.out.println("Hello World");
we can use the println() method to print a line of text to the screen
Java Comments
Comments are used to describe the java code, and make to more readable for other programmer but most of the
time developer also use the comment to run the alternate code or test the code.
Single Line comment
In java programming // “double back slash” are used for the single line comment
Example:
//this is comment
Multi line comment:
For multi line comment we use /* comment */
Here previous /* denotes the start of the comment and post */ denotes the end of the comment.
Example:
/*
this is multi line
Comment
*/
Java Variables
Variables are containers for storing data values.

String - stores text, such as "Hello". String values are put inside the double quotes
int - stores integers (whole numbers), without decimals, such as 145 or -145
float – stores the number 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
Declaration
Syntax:
type variable = value;
Example:
int myNum = 5;
float myFloatNum = 5.99f;
char myLetter = 'D';
boolean myBool = true;
String myText = "Hello";
Final keyword

final int a = 10;


you can add the final keyword if you don't want others (or yourself) to overwrite existing
values (this will declare the variable as "final" or "constant", which means unchangeable
and read-only):
Declare Many Variables

To declare more than one variable of the same type, use a comma-separated list:
int x = 5, y = 6, z = 50;
System.out.println(x + y + z);
Java Identifiers
Java variables must be identified with unique names.These unique names are
called identifiers.
Java identifiers can be short name like x and y or more descriptive name like age, address
etc.
it is recommended to use descriptive names in order to create understandable and
maintainable code:
String address = “Kathmandu”
Naming convention for Unique Identifer
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 can not contain whitespace
■ Names can also begin with $ and _ but not recommended.
■ 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
Java Type Casting
Type casting is process of converting one primitive type to another primitive type.
There is two type of casting:
a. Automatic casting
int a = 10;
double x = 10.0;
b. Manual casting: casting must be done manually by placing the type in parentheses in
front of the value
double x = 10.12;
Int a = (int) x;

You might also like