Sunday, 6 May 2018

Chaudhari Karan

Java basic syntax, how to compile program in java

    No comments:


Lecture 2  Java – Basic Syntax, How to compile program in java

Hello friends, In today lecture we will discuss some basic syntax in java. 

Java basic syntax
Java is a collection of Objects. Before we start you have to know about Object, Class, Instance Variables, and Methods.

Object: Java object is states and behaviors. The object is the instance of a class. Example: A Dog has state  - name, and color, etc. and its behavior – eating, barking, etc.

Class: A class can be defined as it is a collection of state and behaviors.

Instance Variables: Every object has the unique set of the instance variable. For object state created by assigned value of these instance variable.  

Methods: Basically a method is a behavior. A class contains many other methods. Which methods logic is written and data is manipulated and the actions are executed.

Program: Print "Hello World" in java.


Java Program



How to compile and run java program :


Step 1: Open Notepad or Notepad++ and write the program as above.

Step 2: Save the program or file as MyFirstJavaProgram.java, In java file always save with .java extension.

Here most common thing you have to know that program or file save with the name of the main class. In above program, the main class is MyFirstJavaProgram and file save with the MyFirstJavaProgram.java

Step 3: Open the command prompt and go to the directory where you saved your file.


Step 4: Compile the file: javac MyFirstJavaProgram.java

java command prompt


Step 5: After compile run the file: java MyFirstJavaProgram

java command prompt


Output: Hello World

Basic Syntax :


Case Sensitivity: Java is case sensitive, which means like Identifier Hello and hello both meanings is different in Java.

Class Name: In java class name should be in Upper Case latter. If several words used in class so each inner world should be in Upper Case latter. Example: class MyFirstJavaProgram

Program File: In java file name always save with the class name. If the filename and class name would not be same so your program will not be compiled. Example: assume the class name is 
Example. So file save with the Example.java

Methods Name: Each method name should start with the Lower Case letter. If several words are used in the method so each inner world should be in Upper Case. Example : public void myFirstName()

Main Function: Every java program processing start from the main function or method.
public static void main(String [] args)

The identifier in Java :


In Java, every component requires the name. so name used for class, object, variable, and method are known as Identifier.

Here have some points for Identifier :

  • Each identifier begins with the letter (A to Z or a to z), currency character ($), or underscore(_).
  • Identifier cannot be start with any number(12) or operator(+,-,*,/).
  • The identifier is a combination of characters.
  • Keyword not used as an Identifier.
  • An identifier is case sensitive.
  • Legal Identifier : test, $age, _example, _5_name.
  • Illegal Identifier : 25example, +age.

The modifier in Java :


Two types of modifier in java.
  • Access Modifier: public, private, protected, and default.
  • Non-access Modifier: abstract, final strictfp

We will discuss modifier in detail in Modifier chapter.

Variable in Java :


There are some types of variables.
  • Local Variable
  • Class Variable or Static Variable
  • Instance Variable or Non-static Variable

Array in Java :


Arrays are used for store the multiple variables of the same type. An array is an object.

Enums in Java :


Enums introduced in Java 5.0. Enums have one of only a few predefined values. That values in this enumerated list are known as enums.

For reducing the number of bugs in code you can use the enums in your code.

java program


Output: Size: Large

Keyword in Java :


abstract
assert
boolean
break
byte
case
catch
char
class
const
continue
default
do
double
else
enum
extends
final
finally
float
for
goto
if
implements
import
instanceof
int
interface
long
native
new
package
private
protected
public
return
short
static
strictfp
super
switch
synchronized
this
throw
throws
transient
try
void
volatile
while






Comments in Java :

java program

Java supports single line and multiline comments. It is very similar to C and C++. All character inside the comment is ignored by the compiler.

Using Blank Line in Java :


In Java, a line containing only whitespace, possibly with the comment, so it is known as blank line, and java ignores the blank line.

Inheritance :


In java class is derived from the classes. So, if you create a new class and here has already a class that has some code but you need that classes code for your new class. In this situation, you can use the concept of Inheritance.

Inheritance through you can use the code of that class for your new class. You don't write the code again. Example, the existing class is super class and the new class is a subclass(derived class).

Interfaces :


In Java, the interface can be defined as the construct between object can communicate with each other.

Next :  In next lecture discuss chapter Objects and classes.

 


Subscribe to this Blog via Email :