Java Tutorial – Language Syntax and Structure

Java Tutorial – Language Syntax and Structure

In this post we discuss the Java Language Syntax and Structure. We will offer a cursory view of the Java language syntax and general structure of Java source code. It is my hope that this will give you enough knowledge to get you started in the right path in Java. Subsequent posts will provide more detail on Java language syntax and structure.

What’s Covered

  1. Language Syntax and Structure
  2. Java Naming Conventions
    1. Java Separators
    2. Java Integer Literals
    3. Java Long Literals
    4. Java Floating Literals
    5. Java Character Literals
    6. Java Class Literals

Language Syntax and Structure

Java Naming Conventions

Java follows Naming Conventions Standards in order to make the program more readable as hence easier to read and understand.

  • Packages

    Java Packages are always written in lowercase domain name order. For example, if your company was avaldes.com then it would be written as com.avaldes.tutorial the general format is, tld.domain.project.subproject. The TLDs currently in use are .com, .net, .org, .mil, .gov, .edu or one of the two-letter ISO country codes like .us, .in, .ca, .jp, .cn.

    Defining Packages

        package com.avaldes.util;
        

    or

    Use Packages via import

        import org.apache.http.client.HttpClient;
        import org.apache.log4j.Logger;
        import java.nio.channels.FileChannel;
        
  • Classes

    Class names should be nouns, mixed case with the first letter of each internal word capitalized.

        public class Person { ... }
        public class Customer { ... }
        public class RestfulSecurityExample { ... }
        
  • Interfaces

    Interfaces should be capitalized like classes, mixed case with the first letter of each internal word capitalized.

        public interface Circle { ... }
        public interface Serializable { ... }
        public interface Animal { ... }
        
  • Methods

    Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.

        private void eat()
        public boolean getData()
        public void run()
        
  • Variables

    Variable names should be kept as short as possible yet maintain meaning. Variables should be in mixed case with the first letter lowercase and the first letter of each internal word capitalized.

        boolean isActive = false;
        int quantityOnHand = 0;
        int i;
        
  • Constants

    Constant names, by convention, are in all upper case with multiple words delimited by underscope ‘_’ character.

        private static final int MAX_FILES = 25;
        private static final String FILENAME = "output.txt";
        

Java Separators

The Java language makes use of certain special characters as separators. Each of these special characters play an important role in Java.

Curly Braces {}

  • Used to mark the start and end of a code block
  •     public class MyClass {
          ...
        }
        
  • Used to initialize arrays
  •     int dataPoints[] = {10, 7, 8, 12, 17, 28, 39, 65, 28};
        

    or

        int[] dataPoints = {10, 7, 8, 12, 17, 28, 39, 65, 28};
        

Parentheses ()

  • Used in method declaration for list of parameters
  •     public void calcSales(float price, int quantity) {
          ...
        }
        
  • Used in method declaration for empty parameter list
  •     System.out.println();
        
  • Used in conversions (cast)
  •     long lg = 786;
        int i = (int) lg;
        
  • Used in control flow statements to hold expressions
  •     while (i < 10) {
          ...
          i++;
        }   
        

    or

        for (int i=0; i<10; i++) {
          ...
        }     
        

Brackets []

  • Used in array declaration
  •     public int months[] = new int[12];
        
  • Used in dereferencing index values from array
  •     for (int i = 0; i < months.length; i++) {
          System.out.format("Month %d = %d\n", i, months[i]);
        }
        

Angle Brackets <>

  • Used to specify Generics in Java
  •     List<Employee> employees = new ArrayList<Employee>();
        Map<String, Player> players = 
                          new HashMap<String, Player>();
        

Semicolon ;

  • Used for terminating statements in Java and as separators in the for loop
  •     int cars = 0;
        boolean isActive = false;
        for (int i = 0; i < 10; i++) {
          ...                       
        }   
        

Colon :

  • Used in the for loop for collection and array
  •     for (String playerID : playerMap.keySet()) {
          ...                       
        }   
        

Comma ,

  • Used to separate elements in array initializer and parameter separator in methods
  •     int[] dataPoints = {10, 7, 8, 12, 17};
        public void displayAt(int x, int y, String message) {
          ...
        }
        

Period .

  • Used in package names (reverse order) as a separator between the TLD (Top-Level Domain) and domain name, sub-domain, etc and used in classes to separate methods or fields
  •     package com.avaldes.tutorial;
        
        logger.info("Status: " + status);
        

Java Integer Literals

Java Integer Literals are a sequence of digits that represent constant values that are stored in variables.

int intValue = 100;
int octalValue = 0529;
int hexValue = 0xBA9E;

Java Integer Literals in Java 7

Starting in Java 7, the underscore character ‘_’ may be used anywhere between digits in a numeric literal. By doing this the developers of Java have improved the readability of your code.

int intValue = 100000000;
int intValue1 = 100_000_000; // more readable
int hexValue = 0xBABE_CAFE;
int byteValue = 0b0110_0011_1101_1100;
    
int notAllowed = 100_000_000_; // cannot put at the end

Java Long Literals

To distinguish a long from an integer we use the letter L or l. However, L is the preferrable choice since it is more readable than the lowercase l which can easily be confused with the digit 1.

long l1 = 1300L;
long l2 = 76403872093L;

Java Long Literals in Java 7

long l3 = 76_403_872_093L;
long socialSecurity = 182_18_6833L;
long creditCard = 3872_6672_2939_8200L;

Java Floating Literals

Floating-point literals of type float end with the letter F or f. If we are using type of double it should end with the letter D or d.

float f1 = 254.9f;
float f2 = 18249.75F;
double d1 = 27500.29d;
double d2 = 36050.99D;
double d3 = 17_500_800.99D;

Java Character Literals

Literals of type char contain any Unicode (UTF-16) character or an escape sequence enclosed in single quotes.

char c1 = 'a';
char c2 = 'M';
char c3 = '%';
char s1 ='\b';  // backspace
char s2 ='\t';  // tab
char s3 ='\\';  // backslash
char s4 ='\"';  // double quotes
char s5 ='\'';  // single quote    
char s6 ='\n';  // linefeed
char s7 ='\r';  // carriage return

Java Class Literals

Java supports the concept of class literals by appending the keyword .class; for example String.class, double.class, long.class, or our own classes for example, Person.class or Customer.class.

Class PersonObject = Person.class;
Person person = (Person) PersonObject.newInstance();
language_syntax

Core Java Related Tutorials

Please Share Us on Social Media

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *