Java Basic syntax

Source: Internet
Author: User
Tags java keywords

Transferred from original: http://www.yiibai.com/java/java_basic_syntax.html

When considering a Java program can also be defined as a collection of communication objects by calling the other party's method. Now, let's briefly see what classes, objects, methods, and instance variables mean.

    • Object-object has state and behavior. For example: Dogs have state-color, name, reproduction and behavior, bobbing, barking, eating. An object is an instance of a class.

    • Class-A class can be defined as a template that describes the behavior/blue Print/object whose type is supported.

    • Method-a method that is basically a behavior. A class can contain many methods. It is the method by which data manipulation and all actions are executed when the logic is written.

    • Instance variables-Each object has its own unique instance variable. The state of an object is created by the value assigned to these instance variables.

First Java program:

Look at the simple code that prints the word Hello world.

Public Class Myfirstjavaprogram { /* this was my first Java program. * This would print ' Hello world ' as the output */
             
             public staticvoid Main (string []args)  {system.. Println "Hello World" //prints Hello world }}  
            

See below how to save the file, compile and run the program. To do this, follow these steps:

    • Open Notepad, and add the code as above.

    • Save the file as: Myfirstjavaprogram.java.

    • Open a Command Prompt window and go to the directory where you saved the class. Let's say it's C:.

    • Enter ' Javac Myfirstjavaprogram.java ' and press ENTER to compile the code. If there are no errors in the code, at the command prompt, go to the next line (assuming: Path variable settings).

    • Now, enter "Java Myfirstjavaprogram" to run the program.

    • You will be able to see the ' Hello World ' printed on the window.

:>myfirstjavaprogram.:>myfirstjavaprogramHello  World 
Basic syntax:

Keep the following points in mind about Java programs.

    • Case Sensitivity-Java is case-sensitive, meaning that the identifier Hello and hello will have different meanings in Java.

    • Class name-The first letter of all class names should be capitalized.
      If you need to use several words to form the name of a class, the first letter of each inner word should be capitalized.

      such as the class name Myfirstjavaclass

    • Method name-all method names should start with a lowercase letter.
      If several words are used to form the name of a method, the first letter of each inner word should be capitalized.

      For example public void Mymethodname ()

    • program file name-the name of the program file should exactly match the name of the class.
      When saving a file, you should use the class name (remember that Java is case sensitive) and add the '. Java ' name to the end (if the file name and the class name do not conform to the program will not compile) save.
      For example, suppose that ' Myfirstjavaprogram ' is the class name. Then the file should be saved as ' Myfirstjavaprogram.java '

    • public static void Main (String args[])-Java program processing starts with the main () method, which is a mandatory partial entry for each Java program.

Java identifiers:

All Java components require a name. The name used for classes, variables, and methods is called identifiers.

In Java, there are a few identifiers to remember. They are as follows:

    • All identifiers should be in one letter (A to Z or A to Z), currency character ($) or underscore (_).

    • The identifier for the first character after it can have any combination of characters.

    • The keyword cannot be used as an identifier.

    • The most important is that identifiers are case-sensitive.

    • Examples of legal identifiers: Age, $salary, _value, __1_value

    • Examples of illegal identifiers s:123abc,-salary

Java modifier:

Like other languages, it is possible to modify the class, method, etc. by using modifiers. There are two types of modifiers:

    • Access modifiers: default, public, protected, private

    • Non-access modifiers: final, abstract, STRICTFP

More details about modifiers are given in the next section.

Java variables:

We will see the following variable types in Java:

    • Local variables
    • class variables (static variables)
    • Instance variable (non-static variable)
Java Arrays:

An array is an object that stores multiple variables of the same type. However an array itself is an object in the heap. We will learn how to declare, construct and initialize in upcoming chapters.

Java enumeration:

Enumerations are described in Java 5.0. Enumerating one variable is limited to one of only a few predefined values. The value in this enumeration list is called an enumeration.

As you use enumerations, you can reduce the number of error codes.

For example, if you consider a fresh juice store application, it would be possible to limit the size of the glass to small, medium, and large. This will ensure that it will not allow anyone to order with any size smaller, medium or large.

Example:
Class Freshjuice { Enum Freshjuicesize{SMALL,MEDIUM,LARGE} FreshjuicesizeSize;}Public Class Freshjuicetest { Public Static voidMain(StringArgs[]){ FreshjuiceJuice= new freshjuice (); Span class= "PLN" > Juice.= freshjuice. Span class= "Typ" >freshjuicesize.; system.. Println ( "Size:"  + Juice. Size }}       /span>                

The example above will produce the following results:

Size:medium

Note: Enumerations can be declared as themselves or within a class. Methods, variables, constructors can be enumerated within the definition as well.

Java Keywords:

The following lists the reserved words in Java. These reserved words may not be used as constants or variables, or any other identifier names.

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 for Java

Java support single-line and multiline annotations are very similar to C and C + +. All the available characters within any comment are ignored by the Java compiler.

Public Class Myfirstjavaprogram{ /* This is my first Java program. * This would print ' Hello world ' as the "output *" is a example of multi-line comments. */Public   static void main(String []args) { //This is an Example of single line comment/* This is also a example of single line comment. * * System.  Out.  println("Hello World");  }}< /c10> 
Use blank lines:

Contains only spaces, possibly with comment lines, called a blank line, and Java completely ignores it.

Inherited:

In Java, a class can derive from a class. Basically, if you need to create a new class, there are already some classes of code that you need, and then you can derive the new class from the code that already exists.

This concept allows the reuse of existing classes of fields and methods without rewriting the code in a new class. In this case, the existing class is called the parent class and the derived class is called a subclass.

Interface:

In the Java language, an interface can be defined as a connection between objects that communicate with each other. Interfaces play a crucial role when it comes to the concept of inheritance.

An interface-defined method that a derived class (subclass) should use. But the implementation of the method depends entirely on the subclass.

Java Basic syntax

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.