Java Basic Coding And Syntax

April 29, 2017
Categorised in: Java Core
Java is defined in classes
- Each source file defines at least one java class that has the name same as given to the file with extension .java
public class Firstcode { //File name will be: Firstcode.java }
- You can create java code in any text editor. However, sublime text or intellij is preferred.
- java command processes the bytecode which is generated by javac command
//Package Declaration: package com.example; //Class Declaration: public class Firstcode { //Main Method: public static void main( String []args) { //Executable Code System.out.println("Hello World!"); } }
- Package: java classes are typically organized into packages. A package is a global unique string that typically starts with domain name in reverse order.
E.g. if domain name is: pratikkataria.com
package name will be: com.pratikkataria
This keeps the package names globally unique - Class declaration:
- Public -> this class is available to entire application
Note: each source code file must contain one public class - Firstcode -> class name that is regarded as identifier
- main -> single method which is lowercase main.
Note: main method is needed for every console application
String[] args indicate that the method is accepting an array of Strings from console. However, the name args is not fixed and can be changed.
3 keywords: public static void ->
- public: available to entire application
- static: this method can be directly called from the method’s definition (i.e. not through an instance of the class)
- void: method does not returns any value
- Public -> this class is available to entire application
- The code is printing Hello World! to the console
Note: semicolon (;) is like a period. It tells the Java compiler that the statement ends here.
Words of Caution
- Java is case sensitive
- All identifiers must be unique inside their scope.
E.g. helloWorld, HelloWorld, HELLOWORLD are different from each other
- All identifiers must be unique inside their scope.
- White Space or Indentation does not matter.
- Therefore, all statements must end with semicolon
E.g. int a; int b; int c; is valid on a single line and represents 3 different statements
- Therefore, all statements must end with semicolon
- Keywords are reserved and can’t be used as class name or other identifier. Reserved keywords are:
const, goto, true, false, nullabstract, continue, for, new, switch
assert, default, goto, package, synchronized
boolean, do, if, private, this
break, double, implements, protected, throw
byte, else, import, public, throws
case, enum, instanceof, return, transient
catch, extends, int, short, try
char, final, interface, static, void
class, finally, long, strictfp, volatile
const, float, native, super, while - All identifiers must start with alphabet character or underscore.
Naming Conventions in Java
Note: these are not must but if you use them then the code becomes more readable to other Java developers and easier to debug.
- Classes start with uppercase character.
- e.g. class MyClass
- Methods and variables start with lowercase character
- e.g. void doSomething( String strWithThis) { }
- Constants are all uppercase.
- e.g. public static final String LASTNAME=”Becker”;
Pratik Kataria is currently learning Springboot and Hibernate.
Technologies known and worked on: C/C++, Java, Python, JavaScript, HTML, CSS, WordPress, Angular, Ionic, MongoDB, SQL and Android.
Softwares known and worked on: Adobe Photoshop, Adobe Illustrator and Adobe After Effects.