一篇文章带你编写10种语言HelloWorld
0,编程语言排行榜
计算机编程语言众多,世界上大概有600 多种编程语言,但是流行的也就几十种。我们来看下编程语言排行榜,下面介绍两种语言排行榜。
Ⅰ TIOBE 指数
该指数每月更新一次,它监控了近300种语言的变化情况,其依据全球IT工程师,课程和第三方提供的信息进行评分,如Google,Bing,Yahoo!,Wikipedia,Amazon,YouTube和Baidu等流行的搜索引擎用于计算评分。
Ⅱ PYPL 编程语言指数
该指数每月更新一次,其原始数据来源于Google,它通过分析在Google上搜索语言教程的频率来进行评分,搜索语言教程的次数越多,该语言就越受欢迎。
该指数供提供4 种排名,分别是:
程序员在学习一门新的编程语言时,一般喜欢用这个语言编写Hello World
。
入门每一种新的语言, 基本要经过以下3 个步骤:
- 安装编译/运行环境。
- 编写
Hello World
代码。 - 运行代码。
下面介绍10 种常见编程语言的Hello World
。我这里使用的是Linux 系统的Ubuntu 版本。
1,C 语言
Ⅰ 安装gcc 编译器
gcc
是C 语言的编译器,g++
是C++ 的编译器。
一般安装好Ubuntu 系统后,gcc
与g++
都是自带的。为了文章的完整性,这里依然介绍其安装方法。
在Ubuntu 系统中,可以通过安装build-essential
软件包,这个软件包包含了gcc
,g++
,make
等工具。可使用apt
命令来安装这个软件包:
sudo apt install build-essential
Ⅱ 编写代码
C 语言hello world
代码如下,文件名为hello.c
:
// 包含头文件
#include <stdio.h>
// 程序入口,main 函数,返回值类型为int 类型
int main()
{
// 打印字符串
printf("hello world.\n");
// 程序结束
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
包含JRE
和 JVM
,在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.\n";
?>
Ⅲ 运行代码
当通过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
以上命令会安装两个程序,分别是R
和 Rscript
。R
用于交互式,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
,可以让你更快速的进入某种语言,接下来还需要进一步学习其语法结构
,数据类型
,控制流
,函数
,结构体/类
等概念。
这篇文章只是抛砖引玉,希望对你有所帮助。
一篇文章带你编写10种语言HelloWorld的更多相关文章
- 一篇文章带你了解服务器操作系统——Linux简单入门
一篇文章带你了解服务器操作系统--Linux简单入门 Linux作为服务器的常用操作系统,身为工作人员自然是要有所了解的 在本篇中我们会简单介绍Linux的特点,安装,相关指令使用以及内部程序的安装等 ...
- MYSQL(基本篇)——一篇文章带你走进MYSQL的奇妙世界
MYSQL(基本篇)--一篇文章带你走进MYSQL的奇妙世界 MYSQL算是我们程序员必不可少的一份求职工具了 无论在什么岗位,我们都可以看到应聘要求上所书写的"精通MYSQL等数据库及优化 ...
- MYSQL(进阶篇)——一篇文章带你深入掌握MYSQL
MYSQL(进阶篇)--一篇文章带你深入掌握MYSQL 我们在上篇文章中已经学习了MYSQL的基本语法和概念 在这篇文章中我们将讲解底层结构和一些新的语法帮助你更好的运用MYSQL 温馨提醒:该文章大 ...
- 一篇文章带你掌握MyBatis简化框架——MyBatisPlus
一篇文章带你掌握MyBatis简化框架--MyBatisPlus 我们在前面的文章中已经学习了目前开发所需的主流框架 类似于我们所学习的SpringBoot框架用于简化Spring开发,我们的国人大大 ...
- 一篇文章带你了解NoSql数据库——Redis简单入门
一篇文章带你了解NoSql数据库--Redis简单入门 Redis是一个基于内存的key-value结构数据库 我们会利用其内存存储速度快,读写性能高的特点去完成企业中的一些热门数据的储存信息 在本篇 ...
- 一篇文章带你掌握主流数据库框架——MyBatis
一篇文章带你掌握主流数据库框架--MyBatis MyBatis 是一款优秀的持久层框架,它支持自定义 SQL.存储过程以及高级映射. 在之前的文章中我们学习了MYSQL和JDBC,但是这些东西远远不 ...
- 一篇文章带你掌握主流基础框架——Spring
一篇文章带你掌握主流基础框架--Spring 这篇文章中我们将会介绍Spring的框架以及本体内容,包括核心容器,注解开发,AOP以及事务等内容 那么简单说明一下Spring的必要性: Spring技 ...
- 一篇文章带你掌握主流服务层框架——SpringMVC
一篇文章带你掌握主流服务层框架--SpringMVC 在之前的文章中我们已经学习了Spring的基本内容,SpringMVC隶属于Spring的一部分内容 但由于SpringMVC完全针对于服务层使用 ...
- 一篇文章带你掌握主流办公框架——SpringBoot
一篇文章带你掌握主流办公框架--SpringBoot 在之前的文章中我们已经学习了SSM的全部内容以及相关整合 SSM是Spring的产品,主要用来简化开发,但我们现在所介绍的这款框架--Spring ...
随机推荐
- ANTLR随笔(二)
安装ANTLR 作者的电脑是MAC的操作系统macOS Catalina 10.15.2. 安装步骤后linux操作的系统的一样, Windows系统大致步骤一样,但是环境变量等配置有差别,作者很久没 ...
- 字典树基础进阶全掌握(Trie树、01字典树、后缀自动机、AC自动机)
字典树 概述 字典树,又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它 ...
- ClickHouse学习系列之三【配置文件说明】
背景 最近花了些时间看了下ClickHouse文档,发现它在OLAP方面表现很优异,而且相对也比较轻量和简单,所以准备入门了解下该数据库系统.在介绍了安装和用户权限管理之后,本文对其配置文件做下相关的 ...
- 1050 String Subtraction (20分)
Given two strings S1 and S2, S=S1−S2 is defined to be the remaining string after taking ...
- c期末笔记(3)
参数于模运算 1.实参与形参易错点 实参与形参之间是值传递. 实参&形参 实参可以是:常量,表达式或者变量 形参只能是变量 指针和指针变量 1.指针的定义 指针的定义形式:int*p = &a ...
- fastfdfs搭配nginx
fastfdfs搭配nginx 下载fastdfs-nginx-module 模块 wget https://github.com/happyfish100/fastdfs-nginx-module/ ...
- Python 类属性和方法
import types class Dog(object): __slots__ = ("name", "color", "info") ...
- 中阶d03.4 JDBC_DAO
1.环境准备(单项目下用,在大jdbc项目下只用配置一次) jdbc的驱动(mysqlxxjdbc.jar).util工具(包装释放资源.建立连接.访问properties文件等方法) 2.dao的概 ...
- 【Selenium06篇】python+selenium实现Web自动化:日志处理
一.前言 最近问我自动化的人确实有点多,个人突发奇想:想从0开始讲解python+selenium实现Web自动化测试,请关注博客持续更新! 这是python+selenium实现Web自动化第六篇博 ...
- loadrunner post请求
注意:loadrunner参数中的引号,需要自己加"\" post 请求,分为header 和body两个部分处理 header部分比较容易处理,使用函数实现,如web_add_h ...