官术网_书友最值得收藏!

Java syntax

You might have noticed that NetBeans has been yelling at us for a little bit. There's a light bulb and a red dot on the left and some red jittering under our text, a lot like if we had made a spelling error in some text editors. And that's really what we've done. We've made a syntax mistake. There's something clearly wrong with our Java code and NetBeans knows it.

There are two things wrong here. The first is that our code doesn't end with a semicolon. Java doesn't do a good job of reading spaces and carriage returns, so we need to put semicolons at the end of every functional line of code for the same reason that a Morse code operator would send the message "stop" at the end of every line. Let's add a semicolon at the end of our println() statement:

NetBeans has become a little more satisfied; the jittering has decreased, but there's still something wrong as shown in the preceding screenshot.

The issue is that functions in a programming language, just like files on a computer, have a location where they exist. NetBeans isn't sure where to find the println() function that we've attempted to use. So we simply need to tell NetBeans where this function exists. The full path to the println() function starts from the System package, which includes the out class, which has the definition of the println() function. We write that in Java as System.out.println("Hello World!"); as shown in the following code block.

Let's get rid of the extra spaces I created at lines 5, 6, and 7, not because they would affect the way our program runs, but because it doesn't make it look quite as nice. Now we've written our HelloWorld program:

public class HelloWorld { 
    public static void main(String[] args) { 
        System.out.println("Hello World!"); 
    } 
} 
主站蜘蛛池模板: 平罗县| 宿迁市| 龙游县| 拜城县| 阿勒泰市| 亳州市| 巴中市| 柘荣县| 仁怀市| 宽城| 濉溪县| 元谋县| 子长县| 定远县| 广西| 沅江市| 桐柏县| 巢湖市| 四川省| 青河县| 黑山县| 宜州市| 正定县| 湖州市| 呼图壁县| 揭东县| 抚州市| 峨山| 舟曲县| 政和县| 简阳市| 大新县| 江川县| 三亚市| 阳西县| 霍林郭勒市| 涿鹿县| 祁门县| 奉化市| 遂川县| 晋中市|