Core Java - SPLessons

Java Programming Basics

Chapter 4

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java Programming Basics

Java Programming Basics

shape Introduction

The chapter Java Programming Basics will explain the coding block in the code, Java Programming basics explains about a Java type definition block, how to develop Java programs, Java statements, Java errors, Java variables , Java methods.

Defining types

1. class type

shape Description

The “class” type is used to define the concrete methods of the application and can also define abstract method which is known as abstract class. Following is the syntax.
[java]class ClassName
The following is an example.
[java]Class Splessons{
public static void main(String args[]){
System.out.println("Stop thinking Start coding");
In the above example, Splessons is the name of the class and void is the return type and println is used to print the statement.
Output: Now compile the code result will be as follows.
[java]Stop thinking Start coding[/java]

2. interface type

shape Description

The “interface” type is used to define only the abstract method,it can’t be used to develop concrete methods. Following is the syntax.
[java]interface InterfaceName
abstract methods();

The following is an example for the interface.
[java]package com.splessons;

interface printable{
void print();
class Sample implements printable{
public void print(){System.out.println("Welcome To Splessons");}

public static void main(String args[]){
Sample obj = new Sample();
} [/java]
In the above example, Printable interface has only one method, its implementation is provided in the Sample class.
Output: Now compile the code result will be as follows.
[java]Welcome To Splessons[/java]

3. Enum type

shape Description

Enum” type is used to declare the constant values. Following is the syntax.
[java]enum EnumName
Constant data;
Enum is sort safe user can not assign whatever else other than predefined Enum constants to an Enum variable. It is a compiler blunder to allot something else, unlike the public static final variables used in Enum int pattern and Enum String pattern. For more detailed overview on enum Click Here .

4. Annotation type

shape Description

The “annotation” type is used to define or develop predefined keyword. Following is the syntax.
[java]@interface AnnotationName
Userdefined keyword;
The annotation is same as interface with small differences as follows.

Developing Java Programs

shape Description

Programs developed by using Java involves 3 steps.They are:

a) Coding

shape Description

Developing a Java program with Java statements is known as coding. One can develop the Java program in any text editor like notepad, editplus or IDE’s like Eclipse or Netbeans. The general structure of writing Java program is as follows.

shape More Info

The above structure consists of 2 main sections
  1. Class Declaration, done by using a keyword “class”.
  2. Class Definition, enclosed between flower braces { }.

Inside class definition, java statements are written.

b) Complilation

shape Description

Compilation is a process of translating a Java file into a class file (. class). This translation is done by the Java compiler. The compiler reads each gave statements at a time and translates Java statements into byte code.
• Bytecode is saved in a file with the extension “.class”.The filename of the .class is same as class name.
• The file with .class extension is known as class file. Compiler by default stores the class file in the location where the java file is saved.Following is the syntax.

c) Execution

shape Description

The process of running Java class file is known as execution. JVM of Java software is capable of running the class file and giving the output based on the class file.

shape Syntax

java classname

shape More Info

JVM executes each statement sequentially because it is an interpreter. The JVM can start the execution of a class file, if the class file contains the byte code of the main method otherwise the JVM throws error.

Java Statements

shape Description

A Java statement is built with the following programming elements.

Keywords are predefined reserved words and all keywords should be in lower case.

Eg: class,void,abstract,public etc

Identifiers are the names provided by the programmer to identify any methods or variables.All Identifiers should start with an alphabetic character,or an underscore( _ ) after that you can use alphanumeric.

Eg: classname,variable name,method name. For example, int empID;

Litterals are the values used in program.
Numeric Literals: 12, 12.34
String Literals: “SPLessons”
Boolean Literals: true/false
For example, int empID=108;

Java Errors

shape Description

Whenever a Java program is compiled, the compiler generates a class file, only if the program doesn’t violate the syntax and the rules of the compiler. Then the compiler throws an error without generating .class file. Errors occurring at the compilation time is known as compile time error. Any error occurring during execution is known as runtime error. It generally occurs because of logical or flow mistakes.


shape Key Points

  • Class can be defined as a box, inside of this developer can write methods and functions.
  • The annotation will consist of some meta data that utilized to reduce the code.