Documents

Java Program Structure

Categories
Published
of 4
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Related Documents
Share
Description
java programming
Transcript
  10/24/2014Java Program Structure - w3resourcehttp://www.w3resource.com/java-tutorial/java-program-structure.php3/10  Average rating 6 out of 10. Total 56 users rated. <<PreviousNext>> Description Let’s use example of HelloWorld Java program to understand structure and features of class.This program is written on few lines, and its only task is to print “Hello World from Java” on thescreen. Refer the following picture. 1. “package sct”: It is package declaration statement. The package statement defines a name space in whichclasses are stored. Package is used to organize the classes based on functionality. If you omitthe package statement, the class names are put into the default package, which has no name.Package statement cannot appear anywhere in program. It must be first line of your programor you can omit it. 2. “public class HelloWorld”: This line has various aspects of java programming. a.  public: This is access modifier keyword which tells compiler access to class. Various valuesof access modifiers can be public, protected,private or default (no value). b.  class: This keyword used to declare class. Name of class (HelloWorld) followed by this  10/24/2014Java Program Structure - w3resourcehttp://www.w3resource.com/java-tutorial/java-program-structure.php4/10 keyword. 3. Comments section: We can write comments in java in two ways. a.  Line comments: It start with two forward slashes (//) and continue to the end of the currentline. Line comments do not require an ending symbol. b.  Block comments start with a forward slash and an asterisk (/*) and end with an asterisk anda forward slash (*/).Block comments can also extend across as many lines as needed. 4. “public static void main (String [ ] args)”: Its method (Function) named main with string array as argument. a.  public : Access Modifier  b.  static: static is reserved keyword which means that a method is accessible and usable eventhough no objects of the class exist. c.  void: This keyword declares nothing would be returned from method. Method can returnany primitive or object. d.  Method content inside curly braces. { } asdfla;sd 5. System.out.println( Hello World from Java ) : a.  System:It is name of Java utility class. b.  out:It is an object which belongs to System class. c.  println:It is utility method name which is used to send any String to console. d.  “Hello World from Java”:It is String literal set as argument to println method. More Information regarding Java Class: Java is an object-oriented language, which means that it has constructs to represent objects from the real world.Each Java program has at least one class that knows how to do certain things or how to represent some type of object. For example, the simplest class, HelloWorld,knows how to greet the world.Classes in Java may have methods (or functions) and fields (or attributes or properties).Let’s take example of Car object which has various properties like color, max speed etc. along with it has functionslike run and stop. In Java world we will represent it like below: view plainprint? 01. package sct; 02. 03. public   class  Car {  10/24/2014Java Program Structure - w3resourcehttp://www.w3resource.com/java-tutorial/java-program-structure.php5/10 04. 05. private  String color; 06. 07. private  int maxSpeed; 08. 09. public  String carInfo(){ 10. 11. return  color + Max Speed:-  + maxSpeed; 12. 13. } 14. 15. //This is constructor of Car Class 16. 17. Car(String carColor, int speedLimit){ 18. 19. this.color = carColor; 20. 21. this.maxSpeed =speedLimit; 22. 23. } 24. 25. } Lets make a class named CarTestwhich will instantiate the car class object and call carInfo method of it and seeoutput. view plainprint? 01. package sct; 02. 03. //This is car test class to instantiate and call Car objects. 04. 05. public   class  CarTest { 06. 07. public   static  void main(String[] args) { 08. 09. Car maruti = new  Car( Red , 160); 10. 11. Car ferrari = new  Car ( Yellow , 400); 12. 13. System.out.println(maruti.carInfo()); 14. 15. System.out.println(ferrari.carInfo()); 16. 17. } 18. 19. } Output of above CarTest java class is as below. We can run CarTest java program because ithas main method. Main method is starting point for any java program execution. Running aprogram means telling the Java VIrtual Machine (JVM) to Load theclass, then start executingits main () method. Keep running 'til all thecode in main is finished.   10/24/2014Java Program Structure - w3resourcehttp://www.w3resource.com/java-tutorial/java-program-structure.php6/10 Common Programming guidelines: Java identifiers must start with with a letter, a currency character ($), or a connecting character such as theunderscore ( _ ). Identifiers cannot start with a number. After first character identifiers can contain any combination of letters,currency characters, connecting characters, or numbers. For example,int variable1 = 10 ; //This is validint 4var =10 ; // this is invalid, identifier can’t start with digit.Identifiers, method names, class names are case-sensitive; var and Varare two different identifiers.You can’t use Java keywords as identifiers, Below table shows list of Java keywords.abstractbooleanbreakbytecasecatchcharclassconstcontinuedefaultdodoubleclseextendsfinalfinallyfloatforgotoifimplementsimportinstanceof intinterfacelongnativenewpackageprivateprotectedpublicreturnshortstaticstrictfpsuperswitchsynchronizedthisthrowthrowstransienttryvoidvolatilewhileassertenum Classes and interfaces:  The first letter should be capitalized, and if several words are linked together to form thename, the first letter of the inner words should be uppercase (a format that's sometimes called camelCase ). Methods:  The first letter should be lowercase, and then normal camelCaserules should be used.For example:getBalancedoCalculationsetCustomerName Variables:  Like methods, the camelCase format should be used, starting witha lowercase letter. Sun recommendsshort, meaningful names, which sounds good to us. Some examples:buttonWidthaccountBalanceempName Constants:  Java constants are created by marking variables static and final. They should be named using uppercaseletters with underscore characters as separators:MIN_HEIGHTThere can be only one public class per source code file.Comments can appear at the beginning or end of any line in the source code file; they are independent of any of thepositioning rules discussed here.If there is a public class in a file, the name of the file must match the nameof the public class. For example, a classdeclared as “public class Dog { }” must be in a source code file named Dog.java.To understand more about access modifiers applicable to classes, methods, variables in access modifier tutorial.
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks