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 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)
时间: 2024-07-31 12:06:58

C Programming vs. Java Programming的相关文章

Get your Advanced Java Programming Degree with these Tutorials and Courses

Getting started as a Java developer these days is quite straightforward. There are countless books on the subject, and of course an abundance of online material to study. 最近,入门成为一名java开发人员是非常简单的.有无相关的书籍,当然还有大量的在线资料可供学习 Of course, our own site offers

Java Programming Tutorial Java Native Interface (JNI)

1.  Introduction At times, it is necessary to use native codes (C/C++) to overcome the memory management and performance constraints in Java. Java supports native codes via the Java Native Interface (JNI). JNI is difficult, as it involves two languag

WebSocket Java Programming入门-1(annotated)

1.前言 一直没有怎么做过前端的东西,但是最近的项目中,前端人员奇缺,公司又不安排新的人员进入,所以我这个后台开发人员只能拉过来坐前端了,前段的东西感觉一大堆,CSS,js自不必说,HTML生态圈就有很多的技术要去学习,好吧,那就一个一个的学习整理啦,先来说说最近这个项目的前端用到什么技术吧. 1.Restful:DropWizard这个很简单,两天基本上就能拿下 2.Js Framework :AngularJS 这个JS库的确很酷,最近学习的过程中越来越喜欢这个东西了,打算以后再整理一些An

[JAVA Programming] 关于JList的一点笔记

这次写JAVA课的大作业,首先不得不佩服所给的dictionary.txt文件的厉害之处啊,各种大小写.连字符还有各种词组的不同情况在自己测试的时候都中奖了,我该高兴么... 其实要求不高,大概就是一个词典的查询软件,提供了后台词典,只要完成其中的文件I/O,进行String的处理就可以了. 下面其实主要是一些算法问题,查找的话,既然有序(但是从某些角度说,'-'的值要比a-z小啊,但是在dictionary中的顺序却不是这样啊~~所以我暴力地进行了一次QuickSort...)果断O(logn

Chapter 2 The Java Programming Environment

1. Installing the Java Development kit (JDK) 2. Choosing a Develpment Environment 3. Using the Command-Line Tools 4. Using an Integrated Development Environment 5. Running a Graphical Application 6. Building and Running Applets In this chapter, we wi

Java Programming Test Question 3

import java.util.HashSet; public class JPTQuestion3 { public static void main(String[] args) { HashSet shortSet = new HashSet(); for (short i = 0; i < 100; i++) { shortSet.add(i); shortSet.remove(i - 1); } System.out.println(shortSet.size()); } } 输出:

Introduce to Java Programming:NOTE

<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=default"></script> 博客中要分别讨论的java专题: Calendar类 File类 Number类 测试文章的方式来看 计算机中字节(byte)是最小的存储单元,1byte=8bits. 内存:随机访问存储器(RAM).内存中每一个字节都有其

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

运行时进行编程,类似Java的反射.运行时编程和Java反射的对比如下: 1.相同点 都可以实现的功能:获取类信息.属性设置获取.类的动态加载(NSClassFromString(@“className”)).方法的动态调用 下面是iOS中涉及到的相关使用方法 类的动态加载:NSClassFromString(@“className”),方法的动态调用:NSSelectorFormString(@”doSonethingMethod:”) 常见的方法: isKindOfClass: isMemb

Java programming problems

1.使用循环把26个字母按字典顺序存入数组,在不使用另外数组的情况下将其逆序存放,在根据处理后的字符数组创建一个字符串并输出 public static void main(String[] args) {        char a[]=new char[26],c;   //中间变量c        int i;        for(i=0;i<a.length-1;i++){            a[i]=(char)('a'+i);                    }