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 0null, 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.cstack.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的更多相关文章

  1. Java Programming Language Enhancements

    引用:Java Programming Language Enhancements Java Programming Language Enhancements Enhancements in Jav ...

  2. Java Programming Test Question 3

    import java.util.HashSet; public class JPTQuestion3 { public static void main(String[] args) { HashS ...

  3. Java Programming Test Question 2

    public class JPTQuestion2 { public static void main(String[] args) { String s3 = "JournalDev&qu ...

  4. 文本信息“welcome to java programming!”

    import javax.swing.JOptionPanepublic class welcome {public static void main(string[] arg){JOptionPan ...

  5. Java programming language compiler

    https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html\ javac - Java programming l ...

  6. Parallel Programming AND Asynchronous Programming

    https://blogs.oracle.com/dave/ Java Memory Model...and the pragmatics of itAleksey Shipilevaleksey.s ...

  7. Java Programming Guidelines

    This appendix contains suggestions to help guide you in performing low-level program design and in w ...

  8. iOS运行时编程(Runtime Programming)和Java的反射机制对比

    运行时进行编程,类似Java的反射.运行时编程和Java反射的对比如下:   1.相同点   都可以实现的功能:获取类信息.属性设置获取.类的动态加载(NSClassFromString(@“clas ...

  9. Fast Intro To Java Programming (2)

    Java局部变量 局部变量声明在方法.构造方法或者语句块中: 局部变量在方法.构造方法.或者语句块被执行的时候创建,当它们执行完成后,变量将会被销毁: 访问修饰符不能用于局部变量: 局部变量只在声明它 ...

随机推荐

  1. JAR 介绍-百度百科

    JAR(Java Archive,Java 归档文件)是与平台无关的文件格式,它允许将许多文件组合成一个压缩文件.为 J2EE 应用程序创建的 JAR 文件是 EAR 文件(企业 JAR 文件). J ...

  2. Tomcat学习总结(15)—— Tomcat优化时的参数分析

    (1).maxHttpHeaderSize=”8192” 此选项用于配置:来自于客户端请求的Request和Response的HTTP header 的最大长度,以字节计算.如果不设置,该属性为409 ...

  3. getInitParameter方法

    在ServletConfig和ServletContext都有getInitParameter方法, 这两个方法的都能从web.xml中获取参数,但是是有区别的. 1. web.xml文件 <? ...

  4. 从客户端(ASPxFormLayout1$txtRule="<YYYY><MM><DD><XXXX>")中检测到有潜在危险的 Request.Form 值

    在有文本框的值属于这种时<YYYY><MM><DD><XXXX>,会报这个错 在webconfig中加入 <httpRuntime request ...

  5. Spring Security认证配置(三)

    学习本章之前,可以先了解下上篇Spring Security认证配置(二) 本篇想要达到这样几个目的: 1.登录成功处理 2.登录失败处理 3.调用方自定义登录后处理类型 具体配置代码如下: spri ...

  6. Jquery操作属性

    1.attr(name,value):修改单个属性! name :属性名称 value:属性的值 <script> $(function(){ //给div添加一个alt=hello的属性 ...

  7. Visual Studio 2017 插件扩展

    “工具善其事,必先利其器!装好这些插件让vs更上一层楼” ReSharper : 首先的是Resharper,这个基本是目前是我开发过程中必备的工具集,唯一的缺点就是吃内存,所以你的内存要是低于8G, ...

  8. 9102年了,你还在用for循环操作集合?

    本文首发于cdream的个人博客,点击获得更好的阅读体验! 欢迎转载,转载请注明出处. 前段时间公司书架多了一本<Java8 实战>,毕竟久闻lambda的大名,于是借来一阅.这一看,简直 ...

  9. Java - 线程Join与interrupt

    Java多线程系列--“基础篇”08之 join() 概要 本章,会对Thread中join()方法进行介绍.涉及到的内容包括:1. join()介绍2. join()源码分析(基于JDK1.7.0_ ...

  10. Android - AsyncTask你知道多少?

    http://www.cnblogs.com/qlky/p/5658070.html 为什么asyncTask最好在主线程初始化?在子线程怎么办? AsyncTask四个方法的执行顺序? mWorke ...