Docsity

05 creating your first java classes, Study notes of Digital Logic Design and Programming

Partial preview of the text

Download 05 creating your first java classes and more Study notes Digital Logic Design and Programming in PDF only on Docsity!

Amity School of Engineering

B.Tech., CSE(5th^ Sem.)

Java Programming

Topic: Creating Your First Java Classes

ANIL SAROLIYA

  • Java applications : stand-alone programs
    • Console : support character output to DOS window
    • Windowed : create GUIs (graphical user interfaces)
  • Java applets : Programs embedded (fixed) in a Web page

Java Program Types

 Literal string: character sequence enclosed by " "

 Argument: data passed to a method

 Parsing System.out.println("First Java Application")

  • out is a System object
  • println ( ) is a method encapsulated in out object
  • Dot (.) operator separates the class, object & method
  • println ( ) takes literal string as argument, outputs the string and places cursor on next line
  • print () also can be used instead of println (), but it will not place the cursor next line

Understanding the Statement That Prints the Output

Note: Java is case sensitive; the class named System is a completely different from one class named system, SYSTEM, or even sYsTeM.

public class First

5

 All coding takes place within classes  Class defined by using name or identifier (e.g. First )  Class-name must meet four requirements

  • Contain letters, digits, underscores, or dollar signs
  • Begin with a letter, underscore.
  • May not be a Java reserved keyword
  • May not be primitive value: true , false , or nullConvention : begin the class name with uppercase letter, like:

Understanding the First Class

Some more naming conventions….

Legal but Unconventional Class Names…..

Illegal class name..

 Comments: non-executing lines documenting program

 Statements "commented out" ignored by compiler  Two ways to comment

  • Single Line comments: //
  • Block or Multiline comments: / /

Adding Comments To A Class

11

 Java class stored in secondary memory (disk)  Save public class with .java extension

  • Example: First.java
  • Class name and file name must match
  • Java is case-sensitive
Compiling a Java Class

Two-step translation process

  • Source code compiled into bytecode
  • Bytecode interpreted into executable statements  Compiling at the command line
  • Source code file should be in path
  • Type javac followed by name of file
  • Example: javac First.java  The javac command finds the syntax errors in source code, all syntax errors require to be debug in the text editor
Saving a JAVA Class

 To produce the new output, modify text file

  • Open text file that contains existing program
  • Change original literal string
  • Add an additional literal string with new println( )
  • Save file with changes (under same name)
  • Recompile class with javac command
  • Interpret bytecode and execute using java command

Modifying a Java Class

JOptionPane : provides dialog boxes for GUI output  Implementation

  • Import class package containing JOptionPane
    • Example: import javax.swing.JOptionPane;
  • Use showMessageDialog()
    • It is like println() , but has two arguments
    • Pass null and new literal string
  • Must use exit() method to return control to OS

Creating A Java Application Using GUI Output

Syntax errors

  • Concern violation of language rules
  • Captured and highlighted by compiler  Logic error
  • Syntactically correct, but semantically invalid
  • Captured at run-time by operating system (crash)  Example: class ErrorTest
  • Comments in second line not properly closed, in following program:

Correcting Errors and Finding Help