C Programming vs. Java Programming
| Thing | C | Java |
|---|---|---|
| type of language | function oriented | object oriented |
| basic programming unit | function | class = ADT |
| portability of source code | possible with discipline | yes |
| portability of compiled code | no, recompile for each architecture | yes, bytecode is "write once, run anywhere" |
| security | limited | built-in to language |
| compilation | gcc hello.c creates machine language code | javac Hello.java creates Java virtual machine language bytecode |
| linking in the Math library | gcc -lm calculate.c | no special flags needed |
| joint compilation | gcc main.c helper1.c helper2.c | javac Main.java - any dependent files are automatically re-compiled if needed |
| execution | a.out loads and executes program | java Hello interprets byte code |
| hello, world | #include<stdio.h> int main(void) { printf("Hello\n"); return 0; } |
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello"); } } |
| integer types | int usually 32 bit 2's complement; long usually 32 bit 2's complement |
int is 32 bit 2's complement; long is 64 bit 2's complement |
| floating point types | float usually 32 bit; double usually 64 bit |
float is 32 bit IEEE 754 binary floating point; double is 64 bit IEEE 754 |
| boolean type | use int: 0 for false, nonzero for true | boolean is its own type - stores value true or false |
| character type | char is usually 8 bit ASCII | char is 16 bit UNICODE |
| for loops | for (i = 0; i < N; i++) | for (int i = 0; i < N; i++) |
| array declarations | int *a = malloc(N * sizeof(*a)); | int[] a = new int[N]; |
| array size | arrays don't know their own size | a.length |
| strings | '\0'-terminated character array | built-in immutable String data type |
| accessing a library | #include <stdio.h> | import java.io.File; |
| accessing a library function | #include "math.h" x = sqrt(2.2); all function and variables names are global |
x = Math.sqrt(2.2); functions have different namespaces |
| printing to standard output | printf("sum = %d", x); | System.out.println("sum = " + x); |
| formatted printing | printf("avg = %3.2f", avg); | System.out.printf("avg = %3.2f", avg) |
| reading from stdin | scanf("%d", &x); | Java library support, but easier to use our library int x = StdIn.readInt(); |
| memory address | pointer | reference |
| manipulating pointers | *, &, + | no direct manipulation permitted |
| functions | int max(int a, int b) | public static int max(int a, int b) |
| pass-by-value | primitive data types, structs, and pointers are passed by value; array decays to pointer | all primitive data types and references (which includes arrays), are passed by value |
| defining a data structure | struct | class - key difference is language support for defining methods to manipulate data |
| accessing a data structure | a.numerator for elements | a.numerator for instance variables, c = a.plus(b) for methods |
| pointer chasing | x->left->right | x.left.right |
| allocating memory | malloc | new |
| de-allocating memory | free | automatic garbage collection |
| memory allocation of data structures and arrays | heap, stack, data, or bss | heap |
| buffer overflow | segmentation fault, core dump, unpredicatable program | checked run-time error exception |
| declaring constants | const and #define | final |
| variable auto-initialization | not guaranteed | instance variables (and array elements) initialized to 0, null, or false, compile-time error to access uninitialized variables |
| data hiding | opaque pointers and static | private |
| interface method | non-static function | public method |
| data type for generic item | void * | Object |
| casting | anything goes | checked exception at run-time or compile-time |
| demotions | automatic, but might lose precision | must explicitly cast, e.g., to convert from long to int |
| polymorphism | union | inheritence |
| overloading | no | yes for methods, no for operators |
| graphics | use external libraries | Java library support, use our standard drawing library |
| null | NULL | null |
| enumeration | enum | typesafe enum |
| preprocessor | yes | no |
| variable declaration | at beginning of a block | before you use it |
| variable naming conventions | sum_of_squares | sumOfSquares |
| commenting | /* */ | /* */ or // |
| file naming conventions | stack.c, stack.h | Stack.java - file name matches name of class |
| callbacks | pointers to global functions | use interfaces for commmand dispatching |
| variable number of arguments | varargs | String ... |
| assertions | assert | assert |
| exit and return value to OS | exit(1) | System.exit(1) |
C Programming vs. Java Programming的更多相关文章
- Java Programming Language Enhancements
引用:Java Programming Language Enhancements Java Programming Language Enhancements Enhancements in Jav ...
- Java Programming Test Question 3
import java.util.HashSet; public class JPTQuestion3 { public static void main(String[] args) { HashS ...
- Java Programming Test Question 2
public class JPTQuestion2 { public static void main(String[] args) { String s3 = "JournalDev&qu ...
- 文本信息“welcome to java programming!”
import javax.swing.JOptionPanepublic class welcome {public static void main(string[] arg){JOptionPan ...
- Java programming language compiler
https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html\ javac - Java programming l ...
- Parallel Programming AND Asynchronous Programming
https://blogs.oracle.com/dave/ Java Memory Model...and the pragmatics of itAleksey Shipilevaleksey.s ...
- Java Programming Guidelines
This appendix contains suggestions to help guide you in performing low-level program design and in w ...
- iOS运行时编程(Runtime Programming)和Java的反射机制对比
运行时进行编程,类似Java的反射.运行时编程和Java反射的对比如下: 1.相同点 都可以实现的功能:获取类信息.属性设置获取.类的动态加载(NSClassFromString(@“clas ...
- Fast Intro To Java Programming (2)
Java局部变量 局部变量声明在方法.构造方法或者语句块中: 局部变量在方法.构造方法.或者语句块被执行的时候创建,当它们执行完成后,变量将会被销毁: 访问修饰符不能用于局部变量: 局部变量只在声明它 ...
随机推荐
- 第2章 Selenium2-java 测试环境搭建
2.1 Window下环境搭建 2.1.1 安装Java 2.1.2 安装Eclipse (网上资源很多,就不详将了). 2.1.3 下载Java版的Selenium包. 下载地址:http://d ...
- springMVC添加supportedMediaType仍然中文乱码问题
在使用SpringMVC框架的时候,在java程序里面直接用中文字符串返回String类型,会出现中文乱码.而出现乱码还分2种情况,一种是POST请求页面的乱码情况,一种是GET请求页面的乱码情况. ...
- Prometheus+Grafana打造Mysql监控平台
prometheus/node_exporter/mysqld_exporter都是由go语言编写,需要先安装GoLang环境 下载node_exporter(监控服务器的CPU.内存.存储使用情况) ...
- C#Redis Sorted-Sets
一.前戏 Sorted-Sets和Sets类型极为相似,它们都是字符串的集合,都不允许重复的成员出现在一个Set中.它们之间的主要差别是Sorted-Sets中的每一个成员都会有一个分数(score) ...
- Netty 源码剖析之 unSafe.read 方法
目录: NioSocketChannel$NioSocketChannelUnsafe 的 read 方法 首先看 ByteBufAllocator 再看 RecvByteBufAllocator.H ...
- NPM的正确使用方式
registry配置 npm registry原意为记录.登记.登记处的意思,这里指的也就是node包存放的服务器地址. 查看registry -> npm config get registr ...
- 使用WPF教你一步一步实现连连看(三)
这次首先对以前的结构进行了调整: 第一步:把MyButton按钮的属性独立成一个类,放在一个单独的MyButton.cs中,把图片的初始化也放到里面. 调整代码如下: public class MyB ...
- JavaScriptDay2-简单网页表单验证
Html部分 <!-- 注册表单 1-用户名 text 2-密码 password 3-确认密码 password 4-性别 radio 5-爱好 hobby 6-籍贯 select-optio ...
- C++ vector 排序
C++ vector 排序 C++中当 vector 中的数据类型为基本类型时我们调用std::sort函数很容易实现 vector中数据成员的升序和降序排序,然而当vector中的数据类型为自定义结 ...
- HDU1054(KB10-H 最小顶点覆盖)
Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...