Intermediate Java Programming Prog. PDF

Summary

This document provides a basic introduction to Java programming concepts. It covers topics like Java basic syntax, Java identifiers, and Java Modifiers. The document also introduces Java Arrays and Java Enums. Useful for those learning Java programming.

Full Transcript

Java Basic Syntax Program File Name - Name of the program file When we consider a Java program, it can be defined should exactly match the class name. as a collection of objects that communicate via invoking each other's methods. Let us now briefly...

Java Basic Syntax Program File Name - Name of the program file When we consider a Java program, it can be defined should exactly match the class name. as a collection of objects that communicate via invoking each other's methods. Let us now briefly When saving the file, you should save it using the look into what do class, object, methods, and class name (Remember Java is instance variables mean. case sensitive) and append '.java' to the end of the name (if the file name and the  Object - Objects have states and behaviors. class name do not match, your program will not Example: A dog has states - color, name, breed as compile). well as behavior such as wagging their tail, barking, eating. An object is an instance of a class. Example: Assume 'MyFirstJavaProgram' is the class  Class - A class can be defined as a name. Then the file should be saved as template/blueprint that describes the 'MyFirstJavaProgram.java' behavior/state that the object of its type supports. public static void main(String args[]) - Java  Methods - A method is basically a behavior. A program processing starts from class can contain many methods. It is in methods the main() method which is a mandatory part of where the logics are written, data is manipulated every Java program and all the actions are executed. Java Identifiers  Instance Variables - Each object has its unique set All Java components require names. Names used for of instance variables. An object's state is created by classes, variables, and methods are the values assigned to these instance variables. called identifiers. In Java, there are several points to remember about identifiers. They are as follows: All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an underscore (_). After the first character, identifiers can have any combination of characters. Basic Syntax About Java programs, it is very important to keep in A key word cannot be used as an identifier. mind the following points. Most importantly, identifiers are case sensitive. Case Sensitivity - Java is case sensitive, which Examples of legal identifiers: age, $salary, _value, means identifier Hello and hello would have __1_value. different meaning in Java. Examples of illegal identifiers: 123abc, -salary. Class Names - For all class names the first letter Java Modifiers should be in Upper Case. If several words are used Like other languages, it is possible to modify to form a name of the class, each inner word's first classes, methods, etc., by using modifiers. letter should be in Upper Case. There are two categories of modifiers: Example: class MyFirstJavaClass Access Modifiers: default, public , protected, private Method Names - All method names should start with a Lower Case letter. If several words are used Non-access Modifiers: final, abstract, strictfp to form the name of the method, then each inner We will be looking into more details about word's first letter should be in Upper Case. modifiers in the next section Example: public void myMethodName() Java Arrays Arrays are objects that store multiple variables of the same type. However, an array itself is an object on the heap. We will look into how to declare, construct, and initialize in the upcoming chapters. Java Enums Enums were introduced in Java 5.0. Enums restrict a variable to have one of only a few predefined values. The values in this enumerated list are called enums. With the use of enums it is possible to reduce the number of bugs in your code. For example, if we consider an application for a fresh juice shop, it would be possible to restrict the glass size to small, medium, and large. This would make sure that it would not allow anyone to order any size other than small, medium, or large.

Use Quizgecko on...
Browser
Browser