zoukankan      html  css  js  c++  java
  • 一篇文章带你编写10种语言HelloWorld

    0,编程语言排行榜

    计算机编程语言众多,世界上大概有600 多种编程语言,但是流行的也就几十种。我们来看下编程语言排行榜,下面介绍两种语言排行榜。

    TIOBE 指数

    该指数每月更新一次,它监控了近300种语言的变化情况,其依据全球IT工程师,课程和第三方提供的信息进行评分,如Google,Bing,Yahoo!,Wikipedia,Amazon,YouTube和Baidu等流行的搜索引擎用于计算评分。

    PYPL 编程语言指数

    该指数每月更新一次,其原始数据来源于Google,它通过分析在Google上搜索语言教程的频率来进行评分,搜索语言教程的次数越多,该语言就越受欢迎。

    该指数供提供4 种排名,分别是:

    程序员在学习一门新的编程语言时,一般喜欢用这个语言编写Hello World

    入门每一种新的语言, 基本要经过以下3 个步骤:

    1. 安装编译/运行环境。
    2. 编写Hello World代码。
    3. 运行代码。

    下面介绍10 种常见编程语言的Hello World。我这里使用的是Linux 系统的Ubuntu 版本。

    1,C 语言

    Ⅰ 安装gcc 编译器

    gcc 是C 语言的编译器,g++ 是C++ 的编译器。

    一般安装好Ubuntu 系统后,gccg++ 都是自带的。为了文章的完整性,这里依然介绍其安装方法。

    在Ubuntu 系统中,可以通过安装build-essential软件包,这个软件包包含了gccg++make 等工具。可使用apt 命令来安装这个软件包:

    sudo apt install build-essential
    

    Ⅱ 编写代码

    C 语言hello world 代码如下,文件名为hello.c

    // 包含头文件
    #include <stdio.h>	
    
    // 程序入口,main 函数,返回值类型为int 类型
    int main() 
    {
        // 打印字符串
        printf("hello world.
    ");
    
        // 程序结束
        return 0;
    }
    

    Ⅲ 编译运行

    使用gcc 编译代码,生成的可执行文件名为hello

    gcc hello.c -o hello
    

    执行程序:

    >>> ./hello
    hello world.
    

    2,C++

    Ⅰ 安装g++ 编译器

    g++ 的安装可以参见gcc 的安装。

    Ⅱ 编写代码

    C++ hello world 代码如下,文件名为hello.cc

    #include <iostream>     // 包含头文件
    using namespace std;    // 使用命名空间
     
    // 程序入口,main 函数,返回值类型为int 类型
    int main()
    {
        // 输出字符串
        // endl 为换行符
        cout << "hello world." << endl; 
    
        // 程序结束
        return 0;
    }
    

    Ⅲ 编译运行

    使用g++ 编译代码,生成的可执行文件名为hello

    g++ hello.cc -o hello
    

    执行程序:

    >>> ./hello
    hello world.
    

    3,Java

    Ⅰ 安装 JDK

    JDK 包含JREJVM,在Ubuntu 系统中可通过如下命令安装:

    sudo apt install default-jdk
    

    我这里安装的是Java 11,如下:

    >>> java --version
    openjdk 11.0.6 2020-01-14
    OpenJDK Runtime Environment (build 11.0.6+10-post-Ubuntu-1ubuntu118.04.1)
    OpenJDK 64-Bit Server VM (build 11.0.6+10-post-Ubuntu-1ubuntu118.04.1, mixed mode, sharing)
    

    Ⅱ 编写代码

    Java hello world 代码如下,文件名为HelloWorld.java

    /**
     * public class 是Java 程序的主类
     * Java 代码的文件名要与主类的名字相同
     * Java 入口函数main 函数要写在public class 中
     * 每个Java 代码文件中最多只能有一个public class
     */
    public class HelloWorld {
        public static void main(String[] args) {
            // 输出字符串
            System.out.println("hello world.");
        }
    }
    

    Ⅲ 编译运行

    使用javac 编译Java 代码,下面命令将生成一个HelloWorld.class 文件:

    javac HelloWorld.java
    

    HelloWorld.class 文件是Java 字节码文件,使用java 执行Java字节码文件,注意这里不需要带.class 后缀:

    >>> java HelloWorld
    hello world.
    

    4,Python

    Ⅰ 安装Python

    一般Ubuntu 系统中也会自带python,自己安装的话可以使用如下命令安装:

    sudo apt install python
    

    查看python 的版本:

    >>> python -V
    Python 2.7.17
    

    Ⅱ 编写代码

    python hello world 代码只有一行,文件名为hello.py

    # 打印字符串
    print "hello world."
    

    Ⅲ 运行代码

    Python 是动态语言,不许编译即可运行。使用python 命令执行代码:

    >>> python hello.py
    hello world.
    

    也可以在python 终端中即时执行python 代码,如下:

    >>> python
    Python 2.7.17 (default, Nov  7 2019, 10:07:09) 
    [GCC 7.4.0] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> print "hello world." # 打印字符串
    hello world.
    >>> exit()	# 退出python 终端
    

    5,PHP

    Ⅰ 安装PHP 环境

    首先安装php

    sudo apt install php
    

    执行完成后,查看php是否安装成功:

    >>> php -v
    PHP 7.2.24-0ubuntu0.18.04.3 (cli) (built: Feb 11 2020 15:55:52) ( NTS )
    Copyright (c) 1997-2018 The PHP Group
    Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
        with Zend OPcache v7.2.24-0ubuntu0.18.04.3, Copyright (c) 1999-2018, by Zend Technologies
    

    然后安装apache,执行下面命令安装:

    sudo apt install apache2
    

    如何控制apache2:

    service apache2 status		# 查看apache 状态
    service apache2 start		# 启动apache
    service apache2 stop		# 停止apache
    service apache2 restart		# 重启apache
    
    ls -l /etc/apache2/			# apache 配置文件位置
    ls -l /var/www				# web 目录
    

    最后安装apache2 php 模块

    sudo apt install libapache2-mod-php
    

    执行如下命令,启动apache:

    sudo service apache2 start
    

    检测apache 是否启动成功,执行如下curl 命令,如果能够输出内容则代表apache 启动成功:

    curl localhost
    

    Ⅱ 编写代码

    php 代码需要写在<?php ?> 之间,文件名为hello.php,将这个文件放在/var/www/html/ 目录下:

    <?php
        echo "hello world.
    ";
    ?>
    

    Ⅲ 运行代码

    当通过http 协议访问hello.php 路径的时候,php 代码就会执行,使用curl 命令访问hello.php 路径:

    >>> curl localhost/hello.php
    hello world.
    

    6,Golang

    Ⅰ 安装Go 环境

    执行如下命令安装go

    sudo apt install golang-go
    

    执行完成后,查看go 版本:

    >>> go version
    go version go1.10.4 linux/amd64
    

    Ⅱ 编写代码

    go 语言hello world 代码如下,文件名为hello.go

    // 一个可执行的文件, 包名必须为main
    package main
    
    // 引入 fmt 包
    import "fmt"
    
    // 入口函数
    func main() {
        // 打印字符串
        fmt.Println("hello, world.")
    }
    

    Ⅲ 编译运行

    执行go 代码有两种方式,第一种使用go run 命令直接运行go 代码文件:

    >>> go run hello.go 
    hello, world.
    

    第二种是先使用go build 命令编译go 代码文件,生成一个二进制程序,然后再执行二进制程序:

    >>> go build hello.go 
    >>> ./hello 
    hello, world.
    

    7,R 语言

    Ⅰ 安装R 环境

    sudo apt install r-base-core
    

    以上命令会安装两个程序,分别是RRscriptR 用于交互式,Rscript 用于执行R 脚本。

    安装成功后,查看安装的版本:

    >>> R --version
    R version 3.4.4 (2018-03-15) -- "Someone to Lean On"
    Copyright (C) 2018 The R Foundation for Statistical Computing
    Platform: x86_64-pc-linux-gnu (64-bit)
    
    R is free software and comes with ABSOLUTELY NO WARRANTY.
    You are welcome to redistribute it under the terms of the
    GNU General Public License versions 2 or 3.
    For more information about these matters see
    http://www.gnu.org/licenses/.
    
    >>> Rscript --version
    R scripting front-end version 3.4.4 (2018-03-15)
    

    Ⅱ 编写代码

    R 语言hello world 代码如下,文件名为hello.R

    print("hello world.")
    

    Ⅲ 运行代码

    执行R 语言代码,需要使用Rscript 命令:

    >>> Rscript hello.R
    [1] "hello world."
    

    也可以在R 终端中即时执行R 代码,如下:

    >>> R
    R version 3.4.4 (2018-03-15) -- "Someone to Lean On"
    Copyright (C) 2018 The R Foundation for Statistical Computing
    Platform: x86_64-pc-linux-gnu (64-bit)
    
    R is free software and comes with ABSOLUTELY NO WARRANTY.
    You are welcome to redistribute it under certain conditions.
    Type 'license()' or 'licence()' for distribution details.
    
    R is a collaborative project with many contributors.
    Type 'contributors()' for more information and
    'citation()' on how to cite R or R packages in publications.
    
    Type 'demo()' for some demos, 'help()' for on-line help, or
    'help.start()' for an HTML browser interface to help.
    Type 'q()' to quit R.
    
    > print("hello world.")
    [1] "hello world."
    > q()
    Save workspace image? [y/n/c]: n
    

    8,Ruby

    Ⅰ 安装 ruby 环境

    sudo apt install ruby
    

    查看ruby 版本:

    >>> ruby -v
    ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux-gnu]
    

    Ⅱ 编写代码

    ruby 语言hello world 代码如下,文件名为hello.rb

    # 输出字符串
    puts "hello world."
    

    Ⅲ 运行代码

    使用ruby 命令运行代码:

    >>> ruby hello.rb
    hello world.
    

    ruby 也有交互式模式,使用的是irb 命令,如下:

    >>> irb
    irb(main):001:0> puts "hello world."
    hello world.
    => nil
    irb(main):002:0> exit()
    

    9,Lua

    Ⅰ 安装 lua 环境

    我们安装lua5.3

    sudo apt install lua5.3
    

    Ⅱ 编写代码

    lua 语言hello world 代码如下,文件名为hello.lua

    print("hello world.")
    

    Ⅲ 运行代码

    使用lua5.3 命令运行代码:

    >>> lua5.3 hello.lua 
    hello world.
    

    lua 也有交互式终端:

    >>> lua5.3 
    Lua 5.3.3  Copyright (C) 1994-2016 Lua.org, PUC-Rio
    > print("hello world.")
    hello world.
    > os.exit()
    

    10,Scala

    Ⅰ 安装Scala 环境

    Scala 如同语法上简化版的Java,Scala 代码被编译成Java 字节码,运行于JVM 之上。

    运行Scala 程序,首先需要安装JDK 环境,JDK 的安装可参见3 Java 部分。

    执行以下命令安装scala

    sudo apt install scala
    

    查看scala 版本:

    >>> scala -version
    Scala code runner version 2.11.12 -- Copyright 2002-2017, LAMP/EPFL
    

    Ⅱ 编写代码

    scala语言hello world 代码如下,文件名为HelloWorld.scala

    object HelloWorld {
      // 入口函数
      def main(args: Array[String]): Unit = {
        println("hello world.")
      }
    }
    

    Ⅲ 编译运行

    先使用scalac 编译代码,再使用scala 执行程序:

    >>> scalac HelloWorld.scala
    >>> scala HelloWorld
    hello world.
    

    编写hello world,可以让你更快速的进入某种语言,接下来还需要进一步学习其语法结构数据类型控制流函数结构体/类 等概念。

    这篇文章只是抛砖引玉,希望对你有所帮助。

  • 相关阅读:
    HIVE优化学习笔记
    HIVE执行引擎TEZ学习以及实际使用
    端口状态 LISTENING、ESTABLISHED、TIME_WAIT及CLOSE_WAIT详解,以及三次握手四次挥手,滑动窗口(整理转发)
    kafka时间轮简易实现(二)
    kafka时间轮的原理(一)
    JAVA之G1垃圾回收器
    JAVA之垃圾收集器
    JAVA之内存结构
    SparkSQL学习笔记
    Python 学习 --简单购物车程序
  • 原文地址:https://www.cnblogs.com/codeshell/p/12726481.html
Copyright © 2011-2022 走看看