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. 和我一起打造个简单搜索之ElasticSearch集群搭建

    我们所常见的电商搜索如京东,搜索页面都会提供各种各样的筛选条件,比如品牌.尺寸.适用季节.价格区间等,同时提供排序,比如价格排序,信誉排序,销量排序等,方便了用户去找到自己心里理想的商品. 站内搜索对 ...

  2. Ansible-安装-秘钥-部署-使用

    本文转自:https://www.cnblogs.com/ylqh/p/5902259.html ansiblemaster:192.168.74.146 ansibleslave1 :192.168 ...

  3. Spring中使用变量${}的方式进行参数配置

    在使用Spring时,有些情况下,在配置文件中,需要使用变量的方式来配置bean相关属性信息,比如下面的数据库的连接使用了${}的方式进行配置,如下所示: <bean id="data ...

  4. 【转】CSS和SVG中的剪切——clip-path属性和<clipPath>元素

    本文由大漠根据SaraSoueidan的<Clipping in CSS and SVG – The clip-path Property and <clipPath> Elemen ...

  5. 在MVC应用程序中,怎样删除上传的文件

    在ASP.NET MVC应用程序中,怎样删除上传的文件. 由于上传时,真正文件是存储在应用程序某一目录,在数据库表中,只是存储其基本信息.在删除时,需要注意一下,由于没有事务可操作.Insus.NET ...

  6. 未能找到类型或命名空间名List

    解决方法添加引用using System.Collections.Generic;

  7. SQL Server 数据库的鼠标操作

    在数据库中一些操作用鼠标进行可视化操作更方便快捷 一 SQL Server 开启 任务栏——任务管理器——服务——MSSQLSERVER 开启 我的电脑——控制面板——管理工具——服务——MSSQLS ...

  8. Java虚拟机--内存模型与线程

    Java虚拟机--内存模型与线程 高速缓存:处理器要与内存交互,如读取.存储运算结果,而计算机的存储设备和处理器的运算速度差异巨大,所以加入一层读写速度和处理器接近的高速缓存来作为内存和处理器之间的缓 ...

  9. Web前端基础——CSS

    一.CSS概述 css ( cascading style sheets ) 层叠样式表,可以轻松设置网页元素的显示.位置和格式外,甚至还能产生滤镜,图像 淡化,网页淡入淡出的渐变效果,简而言之,cs ...

  10. 撩课-Web大前端每天5道面试题-Day27

    1.浏览器缓存? 浏览器缓存分为强缓存和协商缓存.当客户端请求某个资源时,获取缓存的流程如下: 先根据这个资源的一些 http header 判断它是否命中强缓存, 如果命中,则直接从本地获取缓存资源 ...