3. Basics of Java Syntax
Intro to Java Syntax
Java uses a familiar and easy-to-learn syntax based on the C and C++ programming languages. Here are some key points to keep in mind as you start learning Java:
- Java is a case-sensitive language, meaning the names of variables, methods, and classes must be spelled exactly as they are defined. For example, the name "myVariable" is not the same as "myvariable" or "MyVariable"
- In Java, every statement ends with a semicolon (;). This is used to mark the end of a statement, just as a period is used to mark the end of a sentence in English.
- Java uses curly braces ({ and }) to group statements into code blocks. This creates a logical structure for your code, making it easier to read and understand.
- Java uses the keyword "public" to specify that a method or class can be accessed anywhere in the program. This is an example of an access modifier, which controls the visibility of a method or class.
Creating your first java Program
Once you have installed the JDK, follow these steps to start a Java program in TextPad:
- Open TextPad and click on the "File" menu.
- Select "New" from the File menu to create a new document.
- Type your Java code into the new document.
- Save your Java program by clicking on the "File" menu and selecting "Save As." Give your program a name with a ".java" extension, such as "HelloWorld.java." Remember, the name of the file has to be the name of the class. For this example, ours is "Helloworld."
- Then use the shortcut "CRTL + 1" to compile the code and find any errors. It should say, "Tool completed successfully."
- To run the program, use "CTRL + 2". This will open the command terminal and show whatever code you just created.