Java Basic Syntax

You might also like

Download as pdf
Download as pdf
You are on page 1of 8
‘wi2ar21, 9:28 PM Java Basic Syniax - GeakslorGocks OG GeeksforGeeks Q Data Structures Algorithms Interview Preparation Topic-wise Practice C++ Java Python Java Basic Syntax Difficulty Level: Easy © Last Updated : 05 Mar, 2021 A Java program is a collection of objects, and these objects communicate through method calls to each other to work together. Here is a brief discussion on the Classes and Objects, Method, Instance variables, syntax, and semantics of Java. Basic terminologies in Java Attention reader! Don't stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course. 1. Object: The object is an instance of a class, have Behavior and state. * Example: A car is an object whose states are: brand, colour, number-plate. * Behavior: Running on the road 2, Class: The class is a blueprint(plan) of class objects and status. Play this Game for 1 Minute and see why everyone is ‘addicted, No Install hips: gaoksforgeoks orgljava-basic-syntau?ref=Ibp 18 ‘wi2ar21, 9:28 PM Java Basic Syniax - GeakslorGocks * Example: Blueprint of the house is class. 3. Method: The behavior of an object is the method. * Example: The fuel indicator indicates the amount of fuel left in the car. 4. Instance variables: Every object has its own unique set of instance variables. The state of an object is generally created by the values that are assigned to these instance variables. Example: Steps to compile and run a java program in a console javac GFG. java java GFG public class GFG { public static void main (String[] args) { System.out.printin("GFG!"); + Output GFG! Note: When the class is public, the name of the file should be the class name The Basic Syntax: 1. Comments in Java There are three types of comments in Java. hips: gaoksforgeoks orgljava-basic-syntau?ref=Ibp ‘wi2ar21, 9:28 PM Java Basic Syniax - GeakslorGocks i. Single line Comment // System.out.print1n("GFG!"); ii. Multi-line Comment /* System. out.print1n("GFG!") ; system. out.printIn("Alice!"); 7 Documentation Comment. Also called a doc comment. /** documentation */ 2. Program File Name The name of a program file should exactly match the class name with an extension of java. The name of the file can be other names if the program does not have any public class. Assume you have a public class GFG GFG.java // valid syntax 9fg.java // invalid syntax 3. Case Sensitivity ava is a case-sensitive language, which means that the identifiers AB, Ab, aB, and ab e different in Java. hips: gaoksforgeoks orgljava-basic-syntau?ref=Ibp ‘2a, 920M Java Basic Syriax - GooksforGoeks System.out.println("Alice"); // valid syntax system.out.println("Alice"); // invalid syntax 4, Class Names i, The first letter of the class should be in Uppercase ii. |f several words are used to form a name of the class, each inner word's first letter should be in Upper Case, Underscore are allowed class MyJavaProgram // valid syntax class myJavaProgram // invalid syntax 5. public static void main(String [] args) Java program processing starts with the method main() 6. Method Names i. All the method names should start with a Lower Case letter. ii, If several words are used to form the name of the method, then each first letter of the inner word should be in Upper Case, Underscore are allowed. (This is allowed in java and please correct the below example as well both are correct syntax as per Java but as standard people follow lowercase first char in function name) public void employeeRecords() // valid syntax jublic void EmployeeRecords() // valid syntax 7. Identifiers in java hips: gaoksforgeoks orgljava-basic-syntau?ref=Ibp ‘wi2ar21, 9:28 PM Java Basic Syniax - GeakslorGocks i. Allidentifiers can begin with a letter (Ato Z or atoz) or an underscore _. ii. The first character of identifiers can have any combination of characters. Most importantly identifiers are case-sensitive iv. A keyword cannot be used as an identifier since it is a reserved word and has some special meaning Legal identifiers: MinNumber, total, ak74, hello world, $amount, _under_va Illegal identifiers: 74ak, -amount 8. White-spaces in Java Aline containing only white-spaces, possibly with the comment, is known as a blank line, and the Java compiler totally ignores it 9. Access Modifiers: These modifiers control the scope of class and methods. * Access Modifiers: default, public, protected, private * Non-access Modifiers: final, abstract, strictfp 10. Java Keywords Keywords or Reserved words are the words in a language that are used for some internal process or represent some predefined actions. These words are therefore not allowed to use as variable names or objects. abstract assert boolean break byte case catch char class const continue default do double else enum extends final finally float e” goto if implements import instanceof _—_int interface hitps:wwwn.gaoksforgeoks orgijava-basic syntau?rel=Ibp ‘wi28r21, 9:28 PM long native private protected short static switch synchronized throws transient volatile — while < Previous new public strictfp this try hips: gaoksforgeoks orgljava-basic-syntau?ref=Ibp Java Basie Syniax - GeakslorGooks package return super throw void Like 0 Next > ‘wi2ar21, 9:28 PM Java Basic Syniax - GeakslorGocks RECOMMENDED ARTICLES 01 02 03 04 Abstract Syntax Tree (AST) in Java () Basic Operators in Java Feb1 06 ProcessBuilder in Java to create a basic online Judge 14 Basic Type Base64 Encoding and () Decoding in Java 14, Feb 1 Article Contributed By : mayur_patil @mayur_pati Vote for difficulty Current di Easy Improved By Article Tags actice Tags : y: Easy Normal | Medium | Hard | Expert ashishi994mishra java-basies, Java Jova hips: gaoksforgeoks orgljava-basic-syntau?ret-Ibp Page: 12 3 Basic Calculator Program Using Java 15, Nov 20 Java Program to Get the Basic Fil Attributes Feb 21 How to Create a Basic Color Picker Tool in Android? 21, Oct 20, Maven Lifecycle and Basic Maven Commands 16, Dec ‘wi2ar21, 9:28 PM Java Basic Syniax - GeakslorGocks Report Issue Improve Article Writing code in comment? Please use ide,geeksforgeeks,org, generate link and share the link here, Load Comments oS GeeksforGeeks © SthFloor, A-n8, Sector-136, Noida, Uttar Pradesh - 201305 © feedback@geeksforgeeks.org Company ‘About Us Careers Privacy Policy Contact Us Copyright Policy Web Development Web Tutorials HTML css JavaScript Bootstrap hips: gaoksforgeoks orgljava-basic-syntau?ref=Ibp jeeksforgeeks , Some rights r 900 Learn Algorithms Data Structures Languages CS Subjects Video Tutorials Contribute Write an Article Write Interview Experience Internships Videos

You might also like