一篇文章带你编写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 ...
随机推荐
- [ASP.NET Core MVC] 如何实现运行时动态定义Controller类型?
昨天有个朋友在微信上问我一个问题:他希望通过动态脚本的形式实现对ASP.NET Core MVC应用的扩展,比如在程序运行过程中上传一段C#脚本将其中定义的Controller类型注册到应用中,问我是 ...
- Pytest系列(7) - skip、skipif跳过用例
如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 pytest.mark.sk ...
- vscode 保存自动 格式化eslint 代码
在网上找了很多种方法,大多都没有成功 一下是一种成功的 配置方法: 1) First, you need to install the ESLint extension in the VS code ...
- 1046 Shortest Distance (20分)
The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...
- Redis 笔记(二)—— STRING 常用命令
字符串中不仅仅可以存储字符串,它可以存储以下 3 中类型的值 : 字符串 整数 浮点数 Redis 可以对字符串进行截取等相关操作,对整数.浮点数进行增减操作. 自增自减命令 命令 用例和描述 INC ...
- urllib笔记
在Python 3中,urllib2被合并到了urllib中,叫做urllib.request 和 urllib.error .urllib整个模块分为urllib.request, urllib.p ...
- 剑指Offer系列之题16~题20
目录 16.反转链表 17.合并两个排序的链表 18.树的子结构
- git处理fork的个人库代码与远程项目库待代码同步与合并
由于每个项目组git代码管理规范都不一致,分支开发和fork开发模式.我就说下fork处理的流程吧. 问题点: 我的代码是从自己的远程个人仓库clone的.我的个人远程仓库代码是从项目的远程仓库弄fo ...
- 修改Sysvol复制方式
最近博主在做公司的AD系统升级,首先在做AD系统升级前,一定要认真的调研!!!!在调研是否可升级的过程中 博主发现我司SYSVOL的复制方式还是FRS(没升级前公司是Windows server 20 ...
- CountDownLatch 计算器(具有回调功能)
final CountDownLatch cdl = new CountDownLatch(1); new Thread(new Runnable() { @Override public void ...