CS5303-Computer Science I (Java II)

Java Classes:

  • Use a qualifier, a class keywords, an identifier, and a pair of {}
public class Hello{
   public Hello(){
   public int myInt;
   private float myFloat;
   //This is a constructor
   }
}
  • Whenever a class is instantiated, meaning memory is allocated, the constructor is called.
  • The constructor in Java has the same name as the class, and no return type.
  • One of the primary uses of a constructor is the initialization of member variables.
  • Like a C structure, a Class can contain different fields of data.
  • These fields can be made “public” or “private”
  • The same as C++, private data members cannot be accessed without using a function.
  • Typically, the private data members have a “get” function to get the data.
    int getData()
  • And a set function in order to assign the data.
void setData(int  myParameter)

Collection Data Types

• Vector-A self-expanding linked list.
Deprecated and obsolete.
• HashTable-know this one for the future!
Deprecated and obsolete. (in Java)
• Enumeration-Vectors and other types turn into an enumerated series.
Not used so much anymore
• ArrayList-acts like an array, with a get function

Inheritance:

  • An inheritance means the new class contains all the accessible abilities and traits of its parent class
  • The parent class is the superclass
  • The child class is the subclass
    protected Data Members:

Protected Data Members:

*Not public, not private, but protected

  • Protected data members
  • Private data members are not shared between superclass and subclass
  • Public data members are shared between superclass and subclass
  • Protected data members are shared between subclass, superclass, and classes of the same package.
Sample code:(keyword: Extend)
package Simple;

public class SuperClass{
	private int privateX;
	protected int protectedY;
	public int publicZ;
}

package Simple;
public class Subclass extends SuperClass{
	public void go(){
	//privateX = 5;//Will not work.
	protectedY = 5;
	publicZ = 5;
	}
}

JAVA(tips):

• Every scalar/atomic/basic value in Java is a basic value
• 5, 3.14159, are numbers/literals/values
• Every class in Java is “instantiated” into an object
• Every object is also a memory area
• Every object variable in Java behaves a pointer reference.
• This has complications:
• A basic assignment between objects “=“, only copies the address
• This is a Shallow Copy, only the address is copies
• A more complex assignment uses a “copy constructor”
• This is a Deep Copy, every field must be assigned appropriately

C++ VS Java:

在这里插入图片描述

speed of C++ & Java

  • C, C++ create code for an architecture
  • Java creates code for a Virtual machine.
  • Since the Java runs on the virtual machine, by its nature, it will always be slower than C++.
  • Optimization makes this go faster and faster, but always just a little lag.
  • C++ is faster than Java
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值