CINXE.COM

面试真题:Integer(128)引发的思考-腾讯云开发者社区-腾讯云

<!DOCTYPE html><html lang="zh" munual-autotracker-init="" qct-uid="" qct-pv-id="NJoiE2wksNiASnBpxZAc4" qct-ip="8.222.208.146"><head><meta charSet="UTF-8"/><meta http-equiv="X-UA-Compatible" content="IE=edge"/><meta name="format-detection" content="telephone=no"/><title>面试真题:Integer(128)引发的思考-腾讯云开发者社区-腾讯云</title><meta name="subject" content="其他-空类-腾讯技术创作特训营S10"/><meta name="subjectTime" content="2024-11-26 09:21:07"/><meta name="articleSource" content="O"/><meta name="magicSource" content="N"/><meta name="authorType" content="G,O"/><meta name="productSlug" content="tke"/><meta name="keywords" content="腾讯技术创作特训营S10"/><meta name="description" content="Hey小伙伴们,今天要给大家安利一篇操作实践的文章,相信大家通过仔细阅读,一定会有所收货!"/><meta property="og:title" content="面试真题:Integer(128)引发的思考-腾讯云开发者社区-腾讯云"/><meta property="og:description" content="Hey小伙伴们,今天要给大家安利一篇操作实践的文章,相信大家通过仔细阅读,一定会有所收货!"/><meta property="og:image" content="https://cloudcache.tencentcs.com/open_proj/proj_qcloud_v2/gateway/shareicons/cloud.png"/><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, viewport-fit=cover"/><link rel="dns-prefetch" href="//qccommunity.qcloudimg.com"/><link rel="dns-prefetch" href="//ask.qcloudimg.com"/><link rel="dns-prefetch" href="//cloudcache.tencentcs.com"/><link rel="canonical" href="https://cloud.tencent.com/developer/article/2471045"/><meta name="next-head-count" content="20"/><link rel="stylesheet" href="https://imgcache.qq.com/open_proj/proj_qcloud_v2/gateway/portal/css/base.css"/><link rel="stylesheet" href="https://cloudcache.tencentcs.cn/qcloud/ui/cloud-community/build/base/base-202411191502.css"/><style type="text/css"> .rno-markdown p * { white-space: pre-wrap; word-break: break-all; } .tea-overlay { z-index: 99999!important; } </style><link rel="stylesheet" href="https://cloudcache.tencentcs.cn/qcloud/ui/cloud-community/build/Article/Article-202411221814.css"/><link rel="stylesheet" href="https://cloudcache.tencent-cloud.cn/qcloud/draft-master/dist/draft-master-v2.0.137.d4s2ddo9sb.css"/><link rel="stylesheet" href="https://qccommunity.qcloudimg.com/tc_player/release/tcplayer.min.css"/><script src="https://tam.cdn-go.cn/aegis-sdk/latest/aegis.min.js"></script><script> if (Aegis) { new Aegis({ id: 'dWlmyFvjDnalkbZO8q', env: 'production', onError: true, pagePerformance: true, reportAssetSpeed: true, api: { reportRequest: true, resHeaders: ['x-req-id'], }, reportApiSpeed: true, beforeRequest: function (data) { // load js failed if (data.logType === 'log') { if (data.logs.level === '32' && data.logs.msg.indexOf('google') > -1) return false; } var ignoreKeys = [ 'Script error', 'chrome-extension', 'qq.com', 'queryWeappQrcodeStatus', 'login/ajax/info', 'woa.com', 'trafficCollect.php', 'google', 'dscache', 'act-api', 'set_qc_cookie', 'opc.cloud.tencent.com', 'uc_gre_ad_buss', 'eb.xcj.pub', 'UCShellJava', '/developer/labs/quick/loader', 'edgeImmersiveReaderDOM', 'sendBeacon', 'error-decoder.html', 'qcloud-community-track.min.js' ]; var alarmMsg = [data.logs.url, data.logs.msg].join('|'); for (var i = 0; i < ignoreKeys.length; i++) { if (alarmMsg.indexOf(ignoreKeys[i]) != -1) return false; } if (/bot|wechatdevtools|spider/i.test(navigator.userAgent)) { return false; } if (location.hostname.indexOf('cloud.tencent.') === -1) { return false; } }, }); } </script><link rel="preload" href="https://qccommunity.qcloudimg.com/community/_next/static/css/15864e0bbdb1e0dc.css" as="style"/><link rel="stylesheet" href="https://qccommunity.qcloudimg.com/community/_next/static/css/15864e0bbdb1e0dc.css" data-n-g=""/><link rel="preload" href="https://qccommunity.qcloudimg.com/community/_next/static/css/76e9fe126c1f99d5.css" as="style"/><link rel="stylesheet" href="https://qccommunity.qcloudimg.com/community/_next/static/css/76e9fe126c1f99d5.css" data-n-p=""/><link rel="preload" href="https://qccommunity.qcloudimg.com/community/_next/static/css/02f6d1504ece9578.css" as="style"/><link rel="stylesheet" href="https://qccommunity.qcloudimg.com/community/_next/static/css/02f6d1504ece9578.css" data-n-p=""/><noscript data-n-css=""></noscript><script defer="" nomodule="" src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/polyfills-c67a75d1b6f99dc8.js"></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/webpack-5405fcabd402fccd.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/framework-bae252e255276064.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/main-05fe3e9ebcc3b2a5.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/pages/_app-24edd565aa0d24b3.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/1001-c867a46c1f6346a0.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/900-71009a4f9d961db7.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/743-1dcf6b0fcebaa4b8.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/2191-2e402fdba497a1f2.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/9966-3be3b289121a48f8.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/552-eb32205d1d3e17d7.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/4031-e3e3905dc88f3f92.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/2391-e1b083c8280dce67.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/4117-79ab33e3944a99db.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/113-3bf2d483b3691b04.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/5305-6c6287588b89e496.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/9987-50fa00be52ea0a59.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/6995-71865d1b014e8b9a.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/9179-2e29732c010e5e5b.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/5039-d174dd09ec91e23e.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/7205-134dc657cd46d341.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/787-c20307b99a7c9e79.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/6474-2c0e1629f7738528.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/5650-649e162b184be571.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/9626-02550f5f9bc8270d.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/3346-43205bfde5837dfb.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/1794-9ec55e808fadc7d3.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/3270-57ce21952a43da02.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/8876-7bd73942f530c7f3.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/chunks/pages/article/%5BarticleId%5D-0ebf93c8648222a7.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/5MdUrwfvVsazh9zbVIX0r/_buildManifest.js" defer=""></script><script src="https://qccommunity.qcloudimg.com/community/_next/static/5MdUrwfvVsazh9zbVIX0r/_ssgManifest.js" defer=""></script></head><body class=""><div id="__next"><script src="https://dscache.tencent-cloud.cn/ecache/qcstat/qcloud/qcloudStatApi.js"></script><script src="https://qccommunity.qcloudimg.com/common/exposure-plugin-4.1.15.min.js"></script><script src="https://qccommunity.qcloudimg.com/community-track/qcloud-community-track.min.js"></script><div class="cdc-responsive-layout" aegis-first-screen-timing="true" qct-area="文章详情页"><div class="cdc-article-page cdc-global"><div class="cdc-sticky-header mod-sticky-header is-hidden" style="left:0"><div id="community-top-header-product-container"></div><div class="cdc-sticky-header__inner"><div class="cdc-sticky-header__main"><div class="mod-sticky-header__main"><div class="mod-sticky-header__author"><span class="cdc-avatar circle"><a class="cdc-avatar__inner" style="background-image:url(https://developer.qcloudimg.com/http-save/10011/32d13fd8aae684f33b8b8f8bbc65fdca.jpg)" href="/developer/user/5421023" target="_blank"></a></span><div class="author-info"><a class="author-info__name" href="/developer/user/5421023" target="_blank"><span class="name-text">不惑</span><div class="cdc-emblems__item"></div></a></div></div><div class="mod-sticky-header__split"></div><div class="mod-sticky-header__title"><div class="mod-sticky-header__title-content"><h2 class="title-text">面试真题:Integer(128)引发的思考</h2><span class="title-tag">原创</span></div></div></div></div><div class="cdc-sticky-header__extra"><div class="mod-sticky-header__operates"><button class="cdc-btn mod-sticky-header__operate btn-focus cdc-btn--primary"><i class="add-icon"></i><span>关注作者</span></button></div></div></div></div><div class="cdc-m-guider-banner"><div class="cdc-m-guider-banner__guide-mvp is-detail-page"><div class="cdc-m-guider-banner__guide-mvp-text">前往小程序,Get<em>更优</em>阅读体验!</div><div class="cdc-m-guider-banner__guide-mvp-btn">立即前往</div></div></div><div class="cdc-header cdc-header--block" track="导航"><div class="cdc-header__placeholder"></div><div class="cdc-header__inner"><div id="community-top-header-product-container"></div><div class="cdc-header__top"><div class="cdc-header__top-left"><a href="/?from=20060&amp;from_column=20060" target="_blank" class="cdc-header__top-logo" hotrep="" track="腾讯云官网入口"><i>腾讯云</i></a><div class="cdc-header__top-line"></div><a href="/developer" class="cdc-header__top-logo community"><i>开发者社区</i></a><div class="cdc-header__activity"><div id="cloud-header-product-container"></div></div></div><div class="cdc-header__top-operates"><a href="/document/product?from=20702&amp;from_column=20702" target="_blank" class="cdc-header__link">文档</a><a href="/voc/?from=20703&amp;from_column=20703" target="_blank" class="cdc-header__link">建议反馈</a><a href="https://console.cloud.tencent.com?from=20063&amp;from_column=20063" target="_blank" class="cdc-header__link" track-click="{&quot;areaId&quot;:102001,&quot;subAreaId&quot;:1}">控制台</a><div class="cdc-header__account"><div class="cdc-header__account-inner"><button class="cdc-btn cdc-header__account-btn cdc-btn--primary">登录/注册</button></div></div></div></div><div class="cdc-header__bottom"><div class="cdc-header__bottom-nav"><a href="/developer" class="cdc-header__bottom-home">首页</a><div class="cdc-header__nav-list"><div class="cdc-header__nav-item">学习</div><div class="cdc-header__nav-item">活动</div><div class="cdc-header__nav-item">专区</div><div class="cdc-header__nav-item">工具</div></div><a href="/tvp?from=20154&amp;from_column=20154" class="cdc-header__tvp" target="_blank">TVP</a><div class="cdc-header__activity"><a class="cdc-header__activity-tit" href="/act?from=20061&amp;from_column=20061" target="_blank">最新优惠活动<div class="cdc-badge"><div class="cdc-badge-inner"><div class="cdc-badge-text"></div></div></div></a></div><div id="community-header-product-container"></div></div><div class="cdc-header__bottom-operates"><div class="cdc-header__search"><div class="cdc-search__wrap"><div class="cdc-search"><span class="cdc-search__text">文章/答案/技术大牛</span><button class="cdc-search__btn">搜索<i class="cdc-search__i search"></i></button></div><div class="cdc-search__dropdown"><div class="cdc-search__bar"><input type="text" class="cdc-search__bar-input" placeholder="文章/答案/技术大牛" value=""/><div class="cdc-search__bar-btns"><button class="cdc-search__btn">搜索<i class="cdc-search__i search"></i></button><button class="cdc-search__btn">关闭<i class="cdc-search__i clear"></i></button></div></div></div></div></div><div class="cdc-header__create"><span class="cdc-header__create-btn not-logged"><span class="cdc-svg-icon-con"><span class="cdc-svg-icon" style="width:16px;height:16px"><svg width="16" height="16" viewBox="0 0 16 16" fill="currentcolor" xmlns="http://www.w3.org/2000/svg"><path d="M14.2466 12.0145C14.1698 13.6258 12.8381 14.9131 11.2129 14.9131H11.1579H4.0927H4.03772C2.4125 14.9131 1.08014 13.6258 1.00334 12.0145H1V11.8668V4.07213V4.04627V3.89922H1.00334C1.08014 2.28732 2.4125 1 4.03772 1H9.6473V1.00069H10.0786L8.7688 2.10773H8.43888H7.7916H6.37904H4.03772C2.97234 2.10773 2.10445 2.9777 2.10445 4.04629V4.41869V4.4472V6.39498V11.4269V11.4309V11.8668C2.10445 12.9354 2.97234 13.8053 4.03772 13.8053H6.37904H8.87153H11.2129C12.2782 13.8053 13.1461 12.9355 13.1461 11.8668V11.466V11.454V9.5181V6.39364L14.2506 5.3051V11.8668V12.0145H14.2466ZM10.4324 7.15226L9.63146 7.99761C9.36577 8.2693 8.69326 8.95104 8.48066 9.17631C8.26726 9.40288 8.09039 9.58901 7.95061 9.73544C7.81079 9.88188 7.72667 9.96597 7.70083 9.98656C7.63321 10.0488 7.55703 10.1144 7.47022 10.1846C7.38412 10.2542 7.29404 10.3099 7.20063 10.3516C7.10722 10.4007 6.97072 10.459 6.79049 10.5305C6.61028 10.6001 6.42213 10.6676 6.22468 10.7339C6.02792 10.8002 5.84109 10.8571 5.66484 10.9061C5.48795 10.9538 5.3561 10.9863 5.2693 11.0009C5.08977 11.0214 4.96988 10.993 4.90956 10.9168C4.84931 10.8405 4.83276 10.7107 4.85924 10.5312C4.87315 10.4331 4.9043 10.292 4.95468 10.1078C5.00431 9.92297 5.05802 9.7315 5.11431 9.53341C5.1713 9.33526 5.22629 9.15179 5.27926 8.98484C5.33297 8.8179 5.37599 8.7026 5.40978 8.64032C5.44953 8.54357 5.49463 8.45413 5.54495 8.37399C5.59465 8.29379 5.66616 8.20503 5.75965 8.10766C5.79934 8.06588 5.89281 7.96649 6.03988 7.81018C6.18624 7.65311 6.80114 7.02774 7.02104 6.79783L7.75117 6.03524L8.56212 5.1899L10.6345 3.02466L12.5214 4.93874L10.4324 7.15226ZM13.816 3.58581C13.7166 3.68987 13.6272 3.78064 13.5483 3.85883C13.4694 3.93703 13.4006 4.0066 13.3423 4.06686C13.276 4.13643 13.2144 4.19738 13.1561 4.24903L11.2785 2.33569C11.3785 2.24025 11.4965 2.12565 11.6336 1.99115C11.7707 1.85668 11.8854 1.75061 11.9761 1.67242C12.0934 1.57708 12.2133 1.51013 12.3385 1.47109C12.4525 1.43529 12.5644 1.41805 12.6751 1.41876H12.7056C12.7665 1.42139 12.8268 1.42729 12.8851 1.43724C12.8838 1.4366 12.8811 1.43724 12.8798 1.4366C12.8811 1.4366 12.8838 1.4366 12.8851 1.43724C13.1376 1.48428 13.4019 1.62009 13.6265 1.83743C13.7511 1.95871 13.8524 2.09382 13.9259 2.23296C14.0346 2.43834 14.0863 2.65304 14.0763 2.8491C14.0763 2.87294 14.0783 2.89748 14.0783 2.92201C14.0783 3.03529 14.0571 3.14789 14.0154 3.26055C13.9737 3.37314 13.9067 3.48185 13.816 3.58581Z" fill="#0052D9"></path></svg></span></span>发布<span class="cdc-svg-icon-con cdc-header__create-btn-arrow"><span class="cdc-svg-icon" style="width:16px;height:16px"><svg width="16" height="16" viewBox="0 0 16 16" fill="currentcolor" xmlns="http://www.w3.org/2000/svg"><path d="M8.16377 4L9.57798 5.41421L14.5277 10.364L13.1135 11.7782L8.1638 6.829L3.21402 11.7782L1.7998 10.364L8.16377 4Z"></path></svg></span></span></span></div></div></div></div></div><div class="cdc-m-sticky-header is-hidden is-sticky"><div class="cdc-m-sticky-header__placeholder"></div><div class="cdc-m-sticky-header__main"><div class="cdc-m-sticky-header__con"><div class="cdc-m-sticky-header__trigger"></div><div class="cdc-m-header-article__menu"><div class="cdc-m-header-article__menu-mask"></div><div class="cdc-m-header-article__menu-side"><div class="cdc-m-header__sidebar"><div class="cdc-m-header__sidebar-top"><i class="cdc-m-header__sidebar-top__logo"></i><span class="cdc-m-header__sidebar-top__back"></span></div><div class="cdc-m-header__sidebar-menus"><a href="/developer" class="cdc-m-header__sidebar-menu link">首页</a><div class="tpm1-collapse"><div class="tpm1-collapse__panel"><header class="tpm1-collapse__panel-hd"><div class="tpm1-collapse__panel-title">学习</div></header></div><div class="tpm1-collapse__panel"><header class="tpm1-collapse__panel-hd"><div class="tpm1-collapse__panel-title">活动</div></header></div><div class="tpm1-collapse__panel"><header class="tpm1-collapse__panel-hd"><div class="tpm1-collapse__panel-title">专区</div></header></div><div class="tpm1-collapse__panel"><header class="tpm1-collapse__panel-hd"><div class="tpm1-collapse__panel-title">工具</div></header></div></div><a href="/tvp?from=20154&amp;from_column=20154" class="cdc-m-header__sidebar-menu link">TVP</a><a class="cdc-m-header__sidebar-activity" href="/act?from=20061&amp;from_column=20061" target="_blank">最新优惠活动<div class="cdc-badge"><div class="cdc-badge-inner"><div class="cdc-badge-text"></div></div></div></a></div><div class="cdc-m-header__sidebar-back"><a href="/?from=20060&amp;from_column=20060" class="cdc-m-header__sidebar-back__link"><i></i><span>返回腾讯云官网</span></a></div></div></div></div><div class="cdc-m-sticky-header__author"><span class="cdc-avatar large circle" style="cursor:unset"><span class="cdc-avatar__inner" style="background-image:url(https://developer.qcloudimg.com/http-save/10011/32d13fd8aae684f33b8b8f8bbc65fdca.jpg)"></span></span><div class="cdc-m-sticky-header__author-name">不惑</div><div class="cdc-emblems"><div class="cdc-emblems__item"></div></div></div></div><div class="cdc-m-sticky-header__extra"><div class="cdc-m-sticky-header__extra-icon"><i class="extra-search"></i></div><div class="cdc-m-sticky-header__extra-icon"><i class="extra-share"></i></div><div class="cdc-m-sticky-header__extra-operate"><div class="cdc-m-sticky-header__extra-icon"><i class="extra-man"></i></div></div></div></div></div><div class="cdc-m-header-article"><div class="cdc-m-header-article__placeholder"></div><div class="cdc-m-header-article__content"><div class="cdc-m-header-article__main"><div class="cdc-m-header-article__con"><div class="cdc-m-header-article__trigger"></div><div class="cdc-m-header-article__menu"><div class="cdc-m-header-article__menu-mask"></div><div class="cdc-m-header-article__menu-side"><div class="cdc-m-header__sidebar"><div class="cdc-m-header__sidebar-top"><i class="cdc-m-header__sidebar-top__logo"></i><span class="cdc-m-header__sidebar-top__back"></span></div><div class="cdc-m-header__sidebar-menus"><a href="/developer" class="cdc-m-header__sidebar-menu link">首页</a><div class="tpm1-collapse"><div class="tpm1-collapse__panel"><header class="tpm1-collapse__panel-hd"><div class="tpm1-collapse__panel-title">学习</div></header></div><div class="tpm1-collapse__panel"><header class="tpm1-collapse__panel-hd"><div class="tpm1-collapse__panel-title">活动</div></header></div><div class="tpm1-collapse__panel"><header class="tpm1-collapse__panel-hd"><div class="tpm1-collapse__panel-title">专区</div></header></div><div class="tpm1-collapse__panel"><header class="tpm1-collapse__panel-hd"><div class="tpm1-collapse__panel-title">工具</div></header></div></div><a href="/tvp?from=20154&amp;from_column=20154" class="cdc-m-header__sidebar-menu link">TVP</a><a class="cdc-m-header__sidebar-activity" href="/act?from=20061&amp;from_column=20061" target="_blank">最新优惠活动<div class="cdc-badge"><div class="cdc-badge-inner"><div class="cdc-badge-text"></div></div></div></a></div><div class="cdc-m-header__sidebar-back"><a href="/?from=20060&amp;from_column=20060" class="cdc-m-header__sidebar-back__link"><i></i><span>返回腾讯云官网</span></a></div></div></div></div></div><div class="cdc-m-header-article__title"><div class="cdc-m-header-article__title-logo"></div></div><div class="cdc-m-header-article__extra"><div class="cdc-m-header-article__extra-icon"><i class="extra-search"></i></div><div class="cdc-m-header-article__extra-operate"><div class="cdc-m-header-article__extra-icon"><i class="extra-man"></i></div></div></div></div></div></div><div class="cdc-global__main"><div class="cdc-article__body"><div class="cdc-layout"><div class="cdc-layout__main"><div class="cdc-crumb mod-crumb"><div class="cdc-crumb__inner"><a class="cdc-crumb__item" href="/developer">社区首页</a><span class="cdc-crumb__split"> &gt;</span><a class="cdc-crumb__item" href="/developer/column">专栏</a><span class="cdc-crumb__split"> &gt;</span><span class="cdc-crumb__item current">面试真题:Integer(128)引发的思考</span></div></div><div class="mod-article-content"><div class="mod-header"><div class="mod-header__top"><div class="mod-header__title"><h1 class="title-text">面试真题:Integer(128)引发的思考</h1><span class="title-tag">原创</span></div></div><div class="mod-article-source header"><div class="mod-article-source__main"><div class="mod-article-source__avatar"><img src="https://developer.qcloudimg.com/http-save/10011/32d13fd8aae684f33b8b8f8bbc65fdca.jpg" alt="作者头像"/></div><div class="mod-article-source__detail"><div class="mod-article-source__name"><span>不惑</span><div class="cdc-emblems__item"></div></div></div><button class="cdc-btn mod-article-source__operate cdc-btn--primary"><span><i></i>关注</span></button></div></div><div class="mod-header__bottom"><div class="mod-header__detail"><div class="mod-header__date"><span class="date-text">发布<!-- -->于 <!-- -->2024-11-26 09:21:07</span></div><div class="mod-header__date is-mobile"><span class="date-text">发布<!-- -->于 <!-- -->2024-11-26 09:21:07</span></div><div class="mod-header__infos"><div class="cdc-icon__list"><span class="cdc-svg-icon-con"><span class="cdc-svg-icon" style="width:16px;height:16px"><svg width="16" height="16" viewBox="0 0 16 16" fill="currentcolor" xmlns="http://www.w3.org/2000/svg"><g id="icon-view" transform="translate(0.000000, 3.000000)" fill="currentcolor" fill-rule="nonzero"><path d="M15.885,4.68036 C14.9951,3.57569 11.7987,-0.004272 7.99883,-0.004272 C4.19895,-0.004272 1.02302,3.57569 0.112682,4.68036 C0.040058,4.77107 0.000488281,4.88381 0.000488281,5 C0.000488281,5.1162 0.040058,5.22894 0.112682,5.31964 C1.00767,6.42432 4.20407,10.0043 7.99883,10.0043 C11.7936,10.0043 14.9951,6.42432 15.885,5.31964 C15.9576,5.22894 15.9972,5.1162 15.9972,5 C15.9972,4.88381 15.9576,4.77107 15.885,4.68036 Z M7.99883,8.97632 C4.93029,8.97632 2.25555,6.25043 1.17644,4.99745 C2.25555,3.74446 4.95586,1.01857 7.99883,1.01857 C11.0418,1.01857 13.7421,3.74446 14.8314,4.99745 C13.7421,6.25043 11.0418,8.97632 7.99883,8.97632 Z" id="形状"></path><path d="M7.97304,2.55286 C7.49865,2.55286 7.03491,2.69353 6.64046,2.95709 C6.24602,3.22065 5.93859,3.59525 5.75704,4.03354 C5.5755,4.47182 5.528,4.95409 5.62055,5.41937 C5.7131,5.88465 5.94154,6.31203 6.27699,6.64748 C6.61244,6.98293 7.03982,7.21137 7.5051,7.30392 C7.97038,7.39647 8.45265,7.34897 8.89093,7.16743 C9.32922,6.98588 9.70382,6.67845 9.96738,6.28401 C10.2309,5.88956 10.3716,5.42582 10.3716,4.95143 C10.3716,4.31529 10.1189,3.7052 9.66909,3.25538 C9.21927,2.80556 8.60918,2.55286 7.97304,2.55286 Z M7.97304,6.32716 C7.70095,6.32716 7.43496,6.24647 7.20872,6.09531 C6.98249,5.94414 6.80616,5.72928 6.70203,5.4779 C6.59791,5.22652 6.57066,4.94991 6.62374,4.68304 C6.67683,4.41617 6.80785,4.17104 7.00025,3.97864 C7.19265,3.78625 7.43778,3.65522 7.70465,3.60214 C7.97151,3.54905 8.24813,3.5763 8.49951,3.68042 C8.75089,3.78455 8.96575,3.96088 9.11692,4.18712 C9.26808,4.41335 9.34877,4.67934 9.34877,4.95143 C9.35012,5.13295 9.31553,5.31295 9.247,5.48104 C9.17846,5.64913 9.07734,5.802 8.94946,5.93084 C8.82158,6.05967 8.66946,6.16192 8.50188,6.2317 C8.3343,6.30147 8.15457,6.33739 7.97304,6.33739 L7.97304,6.32716 Z" id="形状"></path></g></svg></span><span class="cdc-svg-icon-text">183</span></span><span class="cdc-svg-icon-con is-comment"><span class="cdc-svg-icon" style="width:16px;height:16px"><svg width="16" height="16" viewBox="0 0 16 16" fill="currentcolor" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M8 13.414L5.58594 11H2V3H14V11H10.4141L8 13.414ZM5.17175 12L8 14.8282L10.8282 12H15V2H1V12H5.17175ZM4 6C3.44775 6 3 6.44769 3 7C3 7.55231 3.44775 8 4 8C4.55225 8 5 7.55231 5 7C5 6.44769 4.55225 6 4 6ZM7 7C7 6.44769 7.44775 6 8 6C8.55225 6 9 6.44769 9 7C9 7.55231 8.55225 8 8 8C7.44775 8 7 7.55231 7 7ZM12 6C11.4478 6 11 6.44769 11 7C11 7.55231 11.4478 8 12 8C12.5522 8 13 7.55231 13 7C13 6.44769 12.5522 6 12 6Z"></path></svg></span><span class="cdc-svg-icon-text">0</span></span></div></div></div><div class="mod-header__operates"><div class="mod-header__operate"><span class="cdc-svg-icon-con is-operate"><span class="cdc-svg-icon" style="width:16px;height:16px"><svg width="16" height="16" viewBox="0 0 16 16" fill="currentcolor" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M9.21101 2.54545C8.80733 1.81818 7.79814 1.81818 7.39446 2.54545L1.94481 12.3636C1.54113 13.0909 2.04573 14 2.85308 14H13.7524C14.5597 14 15.0643 13.0909 14.6607 12.3636L9.21101 2.54545ZM2.85308 12.9091L8.30273 3.09091L13.7524 12.9091H2.85308ZM8.00037 6H9.00037V10H8.00037V6ZM8.00037 11H9.00037V12H8.00037V11Z" fill="currentcolor"></path></svg></span><span class="cdc-svg-icon-text">举报</span></span></div></div></div><div class="mod-header__special"><div class="cdc-special-guide"><span><i class="cdc-special-guide-icon"></i>文章被收录于专栏:</span><a class="cdc-special-guide-name">面经</a></div></div></div><div class="mod-content"><div class="mod-content__markdown"><div><div class="rno-markdown new-version rno-theme-orange"><figure class="theme-block"></figure><p>Hey小伙伴们,今天要给大家安利一篇操作实践的文章,相信大家通过仔细阅读,一定会有所收货!</p><p>🔗【文章链接】:<a style="color:#0052D9" class="" href="https://cloud.tencent.com/developer/article/2470413?from_column=20421&amp;from=20421" target="_blank">『前端必修课』按钮边框的旋转动画</a></p><p>💖 推荐理由:这篇文章是由 <a style="color:#0052D9" class="" href="https://cloud.tencent.com/developer/user/7271764?from_column=20421&amp;from=20421" target="_blank">BNTang </a>撰写的,这篇文章教读者如何使用 CSS 实现旋转按钮边框效果。知识点包括使用 <code>:before</code> 和 <code>:after</code> 元素简化 HTML 结构,结合 <code>transform</code> 创建动画效果,通过 <code>transform-origin</code> 精确控制旋转中心。通过这些方法,够掌握动态效果的实现,提升前端动画设计能力,应用于更复杂的视觉效果中。</p><h2 id="c7fj" name="%E5%BC%95%E8%A8%80">引言</h2><p>在 Java 编程语言中,数据类型的使用至关重要。作为一种静态类型语言,Java 提供了丰富的基本数据类型和对应的包装类。其中,<code>Integer</code> 类是 <code>int</code> 类型的包装类,承载着更复杂的功能,如缓存、装箱和拆箱等。掌握 <code>Integer</code> 类的工作原理,尤其是其装箱和拆箱机制,不仅可以提升程序性能,还能在面试中展示出对 Java 深厚的理解。</p><p>本文将深入探讨 <code>Integer</code> 类的特性、装箱和拆箱的性能影响、常见的面试问题以及在不同版本中的变化,力求为读者提供全面的知识体系。</p><figure class=""><div class="rno-markdown-img-url" style="text-align:center"><div class="rno-markdown-img-url-inner" style="width:100%"><div style="width:100%"><img src="https://developer.qcloudimg.com/http-save/yehe-5421023/d8aa98a8ca8ea0dc6c63a7a66b09e4c0.png" style="width:100%"/></div></div></div></figure><h2 id="7hadk" name="Integer-%E7%B1%BB%E6%A6%82%E8%BF%B0">Integer 类概述</h2><p>Integer 类是 Java 中最常用的包装类之一,提供的一个用于表示整数的类。它实现了 Comparable 接口,允许 Integer 对象进行排序。Integer 类还提供了一些常用的方法,如转换、比较和常量定义。</p><ul class="ul-level-0"><li><strong>范围</strong>:Integer 类型的有效范围为 -2,147,483,648 到 2,147,483,647。</li><li><strong>不可变性</strong>:Integer 对象一旦创建,其值不能更改。</li><li><strong>常用方法</strong>:</li><li><code>compareTo()</code>: 比较两个 <code>Integer</code> 对象的大小。</li><li><code>valueOf()</code>: 返回 <code>int</code> 的 <code>Integer</code> 对象,使用缓存机制。</li><li><code>parseInt()</code>: 将字符串转换为 <code>int</code> 类型。</li></ul><p>Java 提供了自动装箱和拆箱的功能,使得基本类型与其对应的对象类型之间的转换变得更加简单和直观。</p><h3 id="5mrlj" name="%E8%A3%85%E7%AE%B1%E4%B8%8E%E6%8B%86%E7%AE%B1">装箱与拆箱</h3><ul class="ul-level-0"><li><strong>装箱</strong>(Boxing):将基本数据类型转换为其对应的对象类型。自动装箱使得开发者无需显式地创建 <code>Integer</code> 对象。</li></ul><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>java</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-java"><code class="language-java" style="margin-left:0">Integer boxed = 10; // 自动装箱</code></pre></div></div><ul class="ul-level-0"><li><strong>拆箱</strong>(Unboxing):将对象类型转换为基本数据类型。拆箱在使用 <code>Integer</code> 对象时会自动发生。</li></ul><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>java</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-java"><code class="language-java" style="margin-left:0">int primitive = boxed; // 拆箱</code></pre></div></div><p>这两个过程虽然提供了便利,但也伴随着性能开销。</p><h2 id="fan9q" name="%E8%A3%85%E7%AE%B1%E4%B8%8E%E6%8B%86%E7%AE%B1%E7%9A%84%E6%80%A7%E8%83%BD%E5%BD%B1%E5%93%8D">装箱与拆箱的性能影响</h2><h3 id="7cje2" name="%E6%80%A7%E8%83%BD%E5%BC%80%E9%94%80%E5%88%86%E6%9E%90">性能开销分析</h3><h4 id="flbv0" name="%E8%A3%85%E7%AE%B1%E7%9A%84%E5%BC%80%E9%94%80">装箱的开销</h4><p>装箱涉及到内存分配和对象初始化。在频繁的操作中,例如在循环内进行装箱,可能会显著影响性能。在这个例子中,Integer.valueOf(i) 会尝试返回缓存中的对象,但对于大于 127 的值,依然会创建新对象,导致额外的性能开销。</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>java</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-java"><code class="language-java" style="margin-left:0">for (int i = 0; i &lt; 1000000; i++) { Integer boxed = Integer.valueOf(i); // 每次都会创建新对象 }</code></pre></div></div><h4 id="2i3u2" name="%E6%8B%86%E7%AE%B1%E7%9A%84%E5%BC%80%E9%94%80">拆箱的开销</h4><p>拆箱虽然相对开销小,但在大规模数据处理时,多个拆箱操作可能会导致性能问题。在这种情况下,虽然代码简洁,但会产生大量的拆箱操作。</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>java</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-java"><code class="language-java" style="margin-left:0">List&lt;Integer&gt; list = new ArrayList&lt;&gt;(); for (int i = 0; i &lt; 1000000; i++) { list.add(i); // 自动装箱 } for (Integer boxed : list) { int primitive = boxed; // 拆箱,转换为基本类型 }</code></pre></div></div><h3 id="dsi5o" name="%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96%E7%AD%96%E7%95%A5">性能优化策略</h3><p>为了提高性能,可以采取以下措施:</p><ul class="ul-level-0"><li><strong>使用基本类型</strong>:在性能要求高的场景下,尽量使用基本类型而非包装类。</li><li><strong>使用数组</strong>:使用基本类型数组而非 <code>List&lt;Integer&gt;</code>,可以避免装箱和拆箱带来的开销。</li><li><strong>合理使用集合</strong>:在处理大量数据时,可以考虑使用 IntStream 等流式操作,减少装箱和拆箱的频率。</li></ul><p>根据实际情况进行性能测试,比较使用基本类型和包装类的性能。例如,在处理大量数据时,使用 <code>int[]</code> 数组比使用 <code>ArrayList&lt;Integer&gt;</code> 更高效。</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>java</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-java"><code class="language-java" style="margin-left:0">long startTime = System.nanoTime(); for (int i = 0; i &lt; 1000000; i++) { Integer boxed = Integer.valueOf(i); // 装箱 } long endTime = System.nanoTime(); System.out.println(&quot;Boxing time: &quot; + (endTime - startTime) + &quot; ns&quot;); startTime = System.nanoTime(); for (int i = 0; i &lt; 1000000; i++) { int primitive = i; // 直接使用基本类型 } endTime = System.nanoTime(); System.out.println(&quot;Primitive time: &quot; + (endTime - startTime) + &quot; ns&quot;);</code></pre></div></div><figure class=""><div class="rno-markdown-img-url" style="text-align:center"><div class="rno-markdown-img-url-inner" style="width:100%"><div style="width:100%"><img src="https://developer.qcloudimg.com/http-save/yehe-5421023/a7330b259e362e8aa06c4e9061643047.png" style="width:100%"/></div></div></div></figure><h2 id="7bls3" name="%E5%B8%B8%E8%A7%81%E9%9D%A2%E8%AF%95%E9%97%AE%E9%A2%98">常见面试问题</h2><h4 id="99avp" name="%E9%9D%A2%E8%AF%95%E9%97%AE%E9%A2%98%E7%A4%BA%E4%BE%8B">面试问题示例</h4><p><strong>装箱和拆箱的性能开销是什么?</strong></p><p><strong>答案</strong>:装箱和拆箱会产生内存分配和对象创建的开销。在进行装箱时,Java 需要为每个基本类型创建一个新的对象,这可能会导致频繁的内存分配和垃圾回收。在高频率的操作中,特别是在循环中,这种开销会显著影响性能,导致应用程序的响应变慢。</p><p><strong>在 Java 中,如何优化装箱和拆箱的性能?</strong></p><p><strong>答案</strong>:在性能关键的代码中,应优先使用基本类型而非包装类。例如,使用 <code>int</code> 而不是 <code>Integer</code>。在使用集合时,可以考虑使用原始类型数组(如 <code>int[]</code>)或其他高效的数据结构,避免不必要的装箱和拆箱操作。此外,如果必须使用集合,选择合适的容器类型,尽量减少装箱的发生。</p><p><strong>什么情况下会使用装箱和拆箱?</strong></p><p><strong>答案</strong>:装箱和拆箱通常在以下情况下使用:</p><ul class="ul-level-0"><li>当需要使用对象类型时,例如在集合(如 <code>ArrayList&lt;Integer&gt;</code>)中存储元素,Java 会自动进行装箱。</li><li>在使用泛型类型时,基本类型无法直接用于泛型,Java 需要进行装箱以创建对应的对象类型。</li><li>在 API 方法中,如果方法参数是对象类型,而实际传入的是基本类型,Java 会自动进行装箱。</li></ul><h2 id="fee47" name="%E7%BC%93%E5%AD%98%E6%9C%BA%E5%88%B6">缓存机制</h2><p>Java 中的 <code>Integer</code> 类实现了对象缓存机制。这意味着对于 -128 到 127 之间的整数值,Java 会在内存中缓存这些对象,确保同一个值的 <code>Integer</code> 对象是相同的引用。</p><h3 id="6f467" name="%E4%B8%BA%E4%BB%80%E4%B9%88%E9%80%89%E6%8B%A9--128-%E5%88%B0-127-%E7%9A%84%E8%8C%83%E5%9B%B4"><strong>为什么选择 -128 到 127 的范围</strong></h3><p>这个范围的选择是为了在常用的计算中优化内存使用。因为在许多情况下,小整数会被频繁使用,缓存可以显著提高性能,减少内存分配和垃圾回收的压力。</p><h3 id="5fndo" name="%E8%87%AA%E5%8A%A8%E8%A3%85%E7%AE%B1%E5%92%8C%E6%89%8B%E5%8A%A8%E8%A3%85%E7%AE%B1">自动装箱和手动装箱</h3><p>当我们将 <code>int</code> 赋值给 <code>Integer</code> 对象时,Java 会自动进行装箱:</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>java</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-java"><code class="language-java" style="margin-left:0">Integer a = 10; // 自动装箱</code></pre></div></div><p>而手动装箱则是使用 <code>Integer</code> 类的构造函数或静态方法:</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>java</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-java"><code class="language-java" style="margin-left:0">Integer b = Integer.valueOf(10); // 手动装箱</code></pre></div></div><p>在性能上,自动装箱会增加一些开销,但在多数情况下可以忽略。</p><h3 id="8cur5" name="%E6%AF%94%E8%BE%83-Integer-%E5%AF%B9%E8%B1%A1%E7%9A%84%E6%96%B9%E5%BC%8F">比较 Integer 对象的方式</h3><p>比较 <code>Integer</code> 对象时,常见的错误在于使用 <code>==</code> 操作符。使用 <code>==</code> 时,比较的是对象的引用:</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>java</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-java"><code class="language-java" style="margin-left:0">Integer x = 127; Integer y = 127; System.out.println(x == y); // true Integer z = 128; Integer w = 128; System.out.println(z == w); // false</code></pre></div></div><p>为了比较值,应始终使用 <code>equals()</code> 方法:</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>java</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-java"><code class="language-java" style="margin-left:0">System.out.println(z.equals(w)); // true</code></pre></div></div><h3 id="7fqt1" name="%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90">源码分析</h3><p>在 <code>Integer</code> 类中,<code>Integer.valueOf()</code> 方法负责返回指定值的 <code>Integer</code> 对象。对于 -128 到 127 的值,它会返回缓存中的对象;对于其他值,则会新建对象。</p><h4 id="320hu" name="%E5%85%B3%E9%94%AE%E6%BA%90%E7%A0%81%E7%89%87%E6%AE%B5%EF%BC%9A"><strong>关键源码片段</strong>:</h4><figure class=""><div class="rno-markdown-img-url" style="text-align:center"><div class="rno-markdown-img-url-inner" style="width:100%"><div style="width:100%"><img src="https://developer.qcloudimg.com/http-save/yehe-5421023/9afe2ef0e09d344d65f921716ab753bc.png" style="width:100%"/></div></div></div></figure><p>大致意思:</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>java</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-java"><code class="language-java" style="margin-left:0">public static Integer valueOf(int i) { if (i &gt;= -128 &amp;&amp; i &lt;= 127) { return IntegerCache.cache[i + 128]; } return new Integer(i); }</code></pre></div></div><h4 id="6m9c9" name="IntegerCache-%E5%AE%9E%E7%8E%B0%EF%BC%9A"><strong><code>IntegerCache</code></strong> <strong>实现</strong>:</h4><figure class=""><div class="rno-markdown-img-url" style="text-align:center"><div class="rno-markdown-img-url-inner" style="width:100%"><div style="width:100%"><img src="https://developer.qcloudimg.com/http-save/yehe-5421023/9d3187688b7f40146ccd73a43fa97eb7.png" style="width:100%"/></div></div></div></figure><p>大致意思:</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>java</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-java"><code class="language-java" style="margin-left:0">static final class IntegerCache { static final Integer cache[] = new Integer[-(-128) + 127 + 1]; static { for (int i = 0; i &lt; cache.length; i++) { cache[i] = new Integer(i - 128); } } }</code></pre></div></div><h2 id="5cb0k" name="%E9%9D%A2%E8%AF%95%E5%9C%BA%E6%99%AF%E4%B8%AD%E7%9A%84%E5%BA%94%E7%94%A8">面试场景中的应用</h2><p>在面试中,考官可能会询问有关 <code>Integer</code> 类的问题,例如:</p><blockquote><p><strong>“Java 中 <code>Integer</code> 的缓存机制是如何工作的?”</strong></p></blockquote><p>在 Java 中,Integer 类的缓存机制是通过静态内部类 IntegerCache 实现的。Java 会缓存范围在 -128 到 127 之间的 Integer 对象,这意味着在这个范围内,任何相同的整数值都会引用相同的 Integer 对象。这个机制的目的是为了优化性能,减少对象的创建和内存的使用。</p><p>当你使用 Integer.valueOf(int i) 方法时,如果 i 在缓存范围内,方法会直接返回缓存的对象;如果不在范围内,则会创建新的 Integer 对象。这种设计确保了对于常用的小整数的高效处理,避免了不必要的内存分配。</p><blockquote><p><strong>“在 Java 中,使用 <code>==</code> 和 <code>equals()</code> 的区别是什么?”</strong></p></blockquote><p>在 Java 中,== 操作符用于比较两个对象的引用,即它们是否指向同一个内存地址。而 equals() 方法用于比较两个对象的实际内容(值)。</p><p>对于 Integer 类型:</p><ul class="ul-level-0"><li>当你使用 == 比较两个 Integer 对象时,如果它们在缓存范围内(-128 到 127),将返回 true,因为它们引用相同的对象;如果超出范围,可能会返回 false,因为它们是不同的对象。</li><li>使用 equals() 方法始终比较两个对象的值,因此即使两个 Integer 对象是不同的实例,只要它们的值相同,equals() 方法都会返回 true。</li></ul><h2 id="chdtj" name="%E6%80%BB%E7%BB%93">总结</h2><p>在深入探讨 Java 中 <code>Integer</code> 类的特性、装箱与拆箱的性能影响以及相关的最佳实践后,我们可以清晰地认识到,合理使用基本数据类型和包装类对性能优化至关重要。了解 <code>Integer</code> 的缓存机制和比较方式,使我们能够在开发中避免潜在的错误和性能瓶颈。</p><p>对于面试者来说,掌握这些概念不仅能帮助回答相关问题,更能展示对 Java 语言的深入理解。在实际开发中,灵活应用这些知识将显著提升代码的效率和可维护性。</p><p>随着 Java 语言的不断演进,理解这些基本概念将为未来的技术挑战打下坚实基础。希望本文能够为大家提供有价值的见解,帮助在日常编程和职业发展中取得成功。</p></div></div></div><div class="mod-statement"><div class="mod-content__statement"><p>原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。</p><p>如有侵权,请联系 <a href="mailto:cloudcommunity@tencent.com">cloudcommunity@tencent.com</a> 删除。</p></div></div><div class="mod-statement-m"><div class="cdc-tag__list mod-content__tags" track-click=""><div class="cdc-tag" track-click="" track-exposure=""><a class="cdc-tag__inner" href="/developer/tag/18094" target="_blank">腾讯技术创作特训营S10</a></div></div><div class="mod-content__statement"><p>原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。</p><p>如有侵权,请联系 <a href="mailto:cloudcommunity@tencent.com">cloudcommunity@tencent.com</a> 删除。</p></div></div><div class="cdc-tag__list mod-content__tags" track-click=""><div class="cdc-tag" track-click="" track-exposure=""><a class="cdc-tag__inner" href="/developer/tag/18094" target="_blank">腾讯技术创作特训营S10</a></div><div class="cdc-tag is-custom" track-click="" track-exposure=""><a class="cdc-tag__inner" href="/developer/search/article-Integer" target="_blank">#Integer</a></div></div></div></div><div class="mod-article-content is-pill-hidden"><div class="mod-comment"><div class="mod-relevant__title">评论</div><div class="cdc-comment-response"><div class="cdc-comment-response-single-edit not-logged"><div class="cdc-comment-response-single-edit__inner"><span class="cdc-avatar cdc-comment-response-single-edit__avatar cdc-comment__avatar circle"><span class="cdc-avatar__inner" style="background-image:url(https://qcloudimg.tencent-cloud.cn/raw/2eca91c9c29816ff056d22815949d83c.png)" target="_blank"></span></span><div class="cdc-comment-response-single-edit__main"><span>登录</span>后参与评论</div></div></div><div class="cdc-comment-response__toolbar"><div class="cdc-comment-response__number">0<!-- --> 条评论</div><div class="cdc-comment-response__segment"><div class="cdc-comment-response__segment-item is-active">热度</div><div class="cdc-comment-response__segment-item">最新</div></div></div><div class="cdc-comment-response-inner"><div class="cdc-comment-response__body"><div><div class="cdc-loading"><div class="cdc-loading__inner"><div class="cdc-loading__item one"></div><div class="cdc-loading__item two"></div><div class="cdc-loading__item three"></div></div></div></div></div></div><div class="cdc-operate-footer"><div class="cdc-operate-footer__inner"><div class="cdc-operate-footer__toggle is-logout"><div class="cdc-operate-footer__toggle-text"><span>登录 </span>后参与评论</div></div></div></div></div></div></div><div class="mod-article-content recommend"><div class="mod-relevant" qct-area="推荐阅读" qct-exposure=""><div class="mod-relevant__title recommend-read">推荐阅读</div><div class="t-divider t-divider--horizontal" style="margin-bottom:0;margin-top:10px"></div></div></div></div><div class="cdc-layout__side"><div class="cdc-personal-info2 mod-author"><div class="cdc-personal-info2__inner"><div class="cdc-personal-info2__detail"><div class="cdc-personal-info2__main"><div class="cdc-personal-info2__name"><a href="/developer/user/5421023" target="_blank" class="cdc-personal-info2__name-text"></a></div><div class="cdc-personal-info2__level"><div class="cdc-personal-info2__level-number">LV.</div><div class="cdc-emblems cdc-personal-info2__level-emblems"></div></div><div class="cdc-personal-info2__position"></div></div><div class="cdc-personal-info2__avatar"></div></div><div class="cdc-personal-info2__list"><a class="cdc-personal-info2__item" href="/developer/user/undefined/articles" target="_blank"><div class="cdc-personal-info2__item-text">文章</div><div class="cdc-personal-info2__item-number">0</div></a><a class="cdc-personal-info2__item" href="/developer/user/undefined" target="_blank"><div class="cdc-personal-info2__item-text">获赞</div><div class="cdc-personal-info2__item-number">0</div></a></div></div></div><div class="mod-sticky-act"><div class="cdc-directory is-just-commercial"><div class="cdc-directory__wrap"><div class="cdc-directory__inner"><div class="cdc-directory__hd">目录</div><div class="cdc-directory__bd"><div class="cdc-directory__bd-box"><ul class="cdc-directory__list level-1"><li class="cdc-directory__item"><span class="cdc-directory__target" id="menu-c7fj">引言</span></li><li class="cdc-directory__item"><span class="cdc-directory__target" id="menu-7hadk">Integer 类概述</span><ul class="cdc-directory__list level-2"><li class="cdc-directory__item"><span class="cdc-directory__target" id="menu-5mrlj">装箱与拆箱</span><ul class="cdc-directory__list level-3"></ul></li></ul></li><li class="cdc-directory__item"><span class="cdc-directory__target" id="menu-fan9q">装箱与拆箱的性能影响</span><ul class="cdc-directory__list level-2"><li class="cdc-directory__item"><span class="cdc-directory__target" id="menu-7cje2">性能开销分析</span><ul class="cdc-directory__list level-3"><li class="cdc-directory__item"><span class="cdc-directory__target" id="menu-7cje2">装箱的开销</span></li><li class="cdc-directory__item"><span class="cdc-directory__target" id="menu-7cje2">拆箱的开销</span></li></ul></li><li class="cdc-directory__item"><span class="cdc-directory__target" id="menu-dsi5o">性能优化策略</span><ul class="cdc-directory__list level-3"><li class="cdc-directory__item"><span class="cdc-directory__target" id="menu-dsi5o">面试问题示例</span></li></ul></li></ul></li><li class="cdc-directory__item"><span class="cdc-directory__target" id="menu-7bls3">常见面试问题</span></li><li class="cdc-directory__item"><span class="cdc-directory__target" id="menu-fee47">缓存机制</span><ul class="cdc-directory__list level-2"><li class="cdc-directory__item"><span class="cdc-directory__target" id="menu-6f467">为什么选择 -128 到 127 的范围</span><ul class="cdc-directory__list level-3"></ul></li><li class="cdc-directory__item"><span class="cdc-directory__target" id="menu-5fndo">自动装箱和手动装箱</span><ul class="cdc-directory__list level-3"></ul></li><li class="cdc-directory__item"><span class="cdc-directory__target" id="menu-8cur5">比较 Integer 对象的方式</span><ul class="cdc-directory__list level-3"></ul></li><li class="cdc-directory__item"><span class="cdc-directory__target" id="menu-7fqt1">源码分析</span><ul class="cdc-directory__list level-3"><li class="cdc-directory__item"><span class="cdc-directory__target" id="menu-7fqt1">关键源码片段:</span></li><li class="cdc-directory__item"><span class="cdc-directory__target" id="menu-7fqt1">IntegerCache 实现:</span></li></ul></li></ul></li><li class="cdc-directory__item"><span class="cdc-directory__target" id="menu-5cb0k">面试场景中的应用</span></li><li class="cdc-directory__item"><span class="cdc-directory__target" id="menu-chdtj">总结</span></li></ul></div></div></div></div></div><div class="cdc-mod-product2"><div class="cdc-card" qct-exposure="" qct-area="相关产品与服务"><div class="cdc-card__inner"><div class="cdc-card__hd"><div class="cdc-card__title">相关产品与服务</div></div><div class="cdc-card__bd"><div class="cdc-product-info2__list"><div class="cdc-product-info2"><div class="cdc-product-info2__card-main"><div class="cdc-product-info2__card-name">容器服务</div><div class="cdc-product-info2__card-desc">腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。</div><div class="cdc-product-info2__card-list"><a target="_blank" href="https://cloud.tencent.com/product/tke?from=21341&amp;from_column=21341"><i class="product-icon introduce-icon"></i>产品介绍</a><a target="_blank" href="https://cloud.tencent.com/document/product/457?from=21342&amp;from_column=21342"><i class="product-icon document-icon"></i>产品文档</a></div></div><div class="cdc-product-info2__activity"><a target="_blank" href="https://cloud.tencent.com/act/pro/double11-2024?from=21344&amp;from_column=21344"><i class="hot-icon"></i>11.11 上云拼团GO</a></div></div></div></div></div></div></div></div></div></div></div></div><div class="cdc-widget-global"><div class="cdc-widget-global__btn announcement"></div><div class="cdc-widget-global__btn code"><div class="cdc-widget-global__btn-tag">领券</div></div><div class="cdc-widget-global__btn top" style="visibility:hidden"></div></div><div class="cdc-footer"><div class="cdc-footer__inner"><div class="cdc-footer__main"><div class="cdc-footer__website"><ul class="cdc-footer__website-group"><li class="cdc-footer__website-column"><div class="cdc-footer__website-box"><h3 class="cdc-footer__website-title">社区</h3><ul class="cdc-footer__website-list"><li class="cdc-footer__website-item"><a href="/developer/column">技术文章</a></li><li class="cdc-footer__website-item"><a href="/developer/ask">技术问答</a></li><li class="cdc-footer__website-item"><a href="/developer/salon">技术沙龙</a></li><li class="cdc-footer__website-item"><a href="/developer/video">技术视频</a></li><li class="cdc-footer__website-item"><a href="/developer/learning">学习中心</a></li><li class="cdc-footer__website-item"><a href="/developer/techpedia">技术百科</a></li><li class="cdc-footer__website-item"><a href="/developer/zone/list">技术专区</a></li></ul></div></li><li class="cdc-footer__website-column"><div class="cdc-footer__website-box"><h3 class="cdc-footer__website-title">活动</h3><ul class="cdc-footer__website-list"><li class="cdc-footer__website-item"><a href="/developer/support-plan">自媒体同步曝光计划</a></li><li class="cdc-footer__website-item"><a href="/developer/support-plan-invitation">邀请作者入驻</a></li><li class="cdc-footer__website-item"><a href="/developer/article/1535830">自荐上首页</a></li><li class="cdc-footer__website-item"><a href="/developer/competition">技术竞赛</a></li></ul></div></li><li class="cdc-footer__website-column"><div class="cdc-footer__website-box"><h3 class="cdc-footer__website-title">资源</h3><ul class="cdc-footer__website-list"><li class="cdc-footer__website-item"><a href="/developer/specials">技术周刊</a></li><li class="cdc-footer__website-item"><a href="/developer/tags">社区标签</a></li><li class="cdc-footer__website-item"><a href="/developer/devdocs">开发者手册</a></li><li class="cdc-footer__website-item"><a href="/lab?from=20064&amp;from_column=20064">开发者实验室</a></li></ul></div></li><li class="cdc-footer__website-column"><div class="cdc-footer__website-box"><h3 class="cdc-footer__website-title">关于</h3><ul class="cdc-footer__website-list"><li class="cdc-footer__website-item"><a rel="nofollow" href="/developer/article/1006434">社区规范</a></li><li class="cdc-footer__website-item"><a rel="nofollow" href="/developer/article/1006435">免责声明</a></li><li class="cdc-footer__website-item"><a rel="nofollow" href="mailto:cloudcommunity@tencent.com">联系我们</a></li><li class="cdc-footer__website-item"><a rel="nofollow" href="/developer/friendlink">友情链接</a></li></ul></div></li></ul></div><div class="cdc-footer__qr"><h3 class="cdc-footer__qr-title">腾讯云开发者</h3><div class="cdc-footer__qr-object"><img src="https://qcloudimg.tencent-cloud.cn/raw/a8907230cd5be483497c7e90b061b861.png?imageView2/2/w/76" class="cdc-footer__qr-image" alt="扫码关注腾讯云开发者"/></div><div class="cdc-footer__qr-infos"><p class="cdc-footer__qr-info"><span class="cdc-footer__qr-text">扫码关注腾讯云开发者</span></p><p class="cdc-footer__qr-info"><span class="cdc-footer__qr-text">领取腾讯云代金券</span></p></div></div></div><div class="cdc-footer__recommend"><div class="cdc-footer__recommend-rows"><div class="cdc-footer__recommend-cell"><h3 class="cdc-footer__recommend-title">热门产品</h3><div class="cdc-footer__recommend-wrap"><ul class="cdc-footer__recommend-list"><li class="cdc-footer__recommend-item"><a class="com-2-footer-recommend-link" href="https://dnspod.cloud.tencent.com?from=20064&amp;from_column=20064">域名注册</a></li><li class="cdc-footer__recommend-item"><a class="com-2-footer-recommend-link" href="/product/cvm?from=20064&amp;from_column=20064">云服务器</a></li><li class="cdc-footer__recommend-item"><a class="com-2-footer-recommend-link" href="/product/tbaas?from=20064&amp;from_column=20064">区块链服务</a></li><li class="cdc-footer__recommend-item"><a class="com-2-footer-recommend-link" href="/product/mq?from=20064&amp;from_column=20064">消息队列</a></li><li class="cdc-footer__recommend-item"><a class="com-2-footer-recommend-link" href="/product/dsa?from=20064&amp;from_column=20064">网络加速</a></li><li class="cdc-footer__recommend-item"><a class="com-2-footer-recommend-link" href="/product/tencentdb-catalog?from=20064&amp;from_column=20064">云数据库</a></li><li class="cdc-footer__recommend-item"><a class="com-2-footer-recommend-link" href="/product/cns?from=20064&amp;from_column=20064">域名解析</a></li><li class="cdc-footer__recommend-item"><a class="com-2-footer-recommend-link" href="/product/cos?from=20064&amp;from_column=20064">云存储</a></li><li class="cdc-footer__recommend-item"><a class="com-2-footer-recommend-link" href="/product/css?from=20064&amp;from_column=20064">视频直播</a></li></ul></div></div><div class="cdc-footer__recommend-cell"><h3 class="cdc-footer__recommend-title">热门推荐</h3><div class="cdc-footer__recommend-wrap"><ul class="cdc-footer__recommend-list"><li class="cdc-footer__recommend-item"><a class="com-2-footer-recommend-link" href="/product/facerecognition?from=20064&amp;from_column=20064">人脸识别</a></li><li class="cdc-footer__recommend-item"><a class="com-2-footer-recommend-link" href="/product/tm?from=20064&amp;from_column=20064">腾讯会议</a></li><li class="cdc-footer__recommend-item"><a class="com-2-footer-recommend-link" href="/act/pro/enterprise2019?from=20064&amp;from_column=20064">企业云</a></li><li class="cdc-footer__recommend-item"><a class="com-2-footer-recommend-link" href="/product/cdn-scd?from=20064&amp;from_column=20064">CDN加速</a></li><li class="cdc-footer__recommend-item"><a class="com-2-footer-recommend-link" href="/product/trtc?from=20064&amp;from_column=20064">视频通话</a></li><li class="cdc-footer__recommend-item"><a class="com-2-footer-recommend-link" href="/product/tiia?from=20064&amp;from_column=20064">图像分析</a></li><li class="cdc-footer__recommend-item"><a class="com-2-footer-recommend-link" href="/product/cdb?from=20064&amp;from_column=20064">MySQL 数据库</a></li><li class="cdc-footer__recommend-item"><a class="com-2-footer-recommend-link" href="/product/symantecssl?from=20064&amp;from_column=20064">SSL 证书</a></li><li class="cdc-footer__recommend-item"><a class="com-2-footer-recommend-link" href="/product/asr?from=20064&amp;from_column=20064">语音识别</a></li></ul></div></div><div class="cdc-footer__recommend-cell"><h3 class="cdc-footer__recommend-title">更多推荐</h3><div class="cdc-footer__recommend-wrap"><ul class="cdc-footer__recommend-list"><li class="cdc-footer__recommend-item"><a class="com-2-footer-recommend-link" href="/solution/data_protection?from=20064&amp;from_column=20064">数据安全</a></li><li class="cdc-footer__recommend-item"><a class="com-2-footer-recommend-link" href="/product/clb?from=20064&amp;from_column=20064">负载均衡</a></li><li class="cdc-footer__recommend-item"><a class="com-2-footer-recommend-link" href="/product/sms?from=20064&amp;from_column=20064">短信</a></li><li class="cdc-footer__recommend-item"><a class="com-2-footer-recommend-link" href="/product/ocr?from=20064&amp;from_column=20064">文字识别</a></li><li class="cdc-footer__recommend-item"><a class="com-2-footer-recommend-link" href="/product/vod?from=20064&amp;from_column=20064">云点播</a></li><li class="cdc-footer__recommend-item"><a class="com-2-footer-recommend-link" href="https://tm.cloud.tencent.com?from=20064&amp;from_column=20064">商标注册</a></li><li class="cdc-footer__recommend-item"><a class="com-2-footer-recommend-link" href="/solution/la?from=20064&amp;from_column=20064">小程序开发</a></li><li class="cdc-footer__recommend-item"><a class="com-2-footer-recommend-link" href="/product/cat?from=20064&amp;from_column=20064">网站监控</a></li><li class="cdc-footer__recommend-item"><a class="com-2-footer-recommend-link" href="/product/cdm?from=20064&amp;from_column=20064">数据迁移</a></li></ul></div></div></div></div><div class="cdc-footer__copyright"><div class="cdc-footer__copyright-text"><p>Copyright © 2013 - <!-- -->2024<!-- --> Tencent Cloud. All Rights Reserved. 腾讯云 版权所有 </p><p>深圳市腾讯计算机系统有限公司 ICP备案/许可证号:<a href="https://beian.miit.gov.cn/#/Integrated/index" target="_blank">粤B2-20090059 </a><a href="https://www.beian.gov.cn/portal/index.do" target="_blank">深公网安备号 44030502008569</a></p><p>腾讯云计算(北京)有限责任公司 京ICP证150476号 |  <a href="https://beian.miit.gov.cn/#/Integrated/index" target="_blank">京ICP备11018762号</a> <!-- -->|<!-- --> <a href="https://www.beian.gov.cn/portal/index.do" target="_blank">京公网安备号11010802020287</a></p></div></div></div></div><div style="display:none"><a href="/developer/ask/archives.html">问题归档</a><a href="/developer/column/archives.html">专栏文章</a><a href="/developer/news/archives.html">快讯文章归档</a><a href="/developer/information/all.html">关键词归档</a><a href="/developer/devdocs/archives.html">开发者手册归档</a><a href="/developer/devdocs/sections_p1.html">开发者手册 Section 归档</a></div><div class="cdc-m-footer"><div class="cdc-m-footer__inner"><div class="cdc-m-footer__copyright"><p>Copyright © 2013 - <!-- -->2024<!-- --> Tencent Cloud.</p><p>All Rights Reserved. 腾讯云 版权所有</p></div></div></div><div class="cdc-operate-footer"><div class="cdc-operate-footer__inner"><div class="cdc-operate-footer__toggle is-logout"><div class="cdc-operate-footer__toggle-text"><span>登录 </span>后参与评论</div></div><div class="cdc-operate-footer__operations"><div class="cdc-operate-footer__operate"><i class="cdc-operate-footer__operate-icon comment"></i></div><div class="cdc-operate-footer__operate emoji"><div class="emoji-item"><span class="emoji-item-icon update"></span></div></div><div class="cdc-operate-footer__operate"><i class="cdc-operate-footer__operate-icon book"></i></div><div class="cdc-operate-footer__operate"><i class="cdc-operate-footer__operate-icon menu"></i></div><div class="cdc-operate-footer__operate"><i class="cdc-operate-footer__operate-icon more"></i></div></div></div></div><div class="cdc-suspend-pill"><div class="cdc-suspend-pill__inner"><button class="cdc-icon-btn cdc-suspend-pill__item emoji cdc-icon-btn--text"><div class="emoji-item"><span class="emoji-item-icon fire"></span></div><span class="cdc-suspend-pill__item-number">0</span></button><button class="cdc-icon-btn cdc-suspend-pill__item like cdc-icon-btn--text"><span class="cdc-svg-icon-con"><span class="cdc-svg-icon" style="width:24px;height:24px"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="currentcolor"><path fill-rule="evenodd" clip-rule="evenodd" d="M17.5 11.25C17.5 11.9404 16.9404 12.5 16.25 12.5C15.5596 12.5 15 11.9404 15 11.25C15 10.5596 15.5596 10 16.25 10C16.9404 10 17.5 10.5596 17.5 11.25Z M12.25 12.5C12.9404 12.5 13.5 11.9404 13.5 11.25C13.5 10.5596 12.9404 10 12.25 10C11.5596 10 11 10.5596 11 11.25C11 11.9404 11.5596 12.5 12.25 12.5Z M8.25 12.5C8.94036 12.5 9.5 11.9404 9.5 11.25C9.5 10.5596 8.94036 10 8.25 10C7.55964 10 7 10.5596 7 11.25C7 11.9404 7.55964 12.5 8.25 12.5Z M5 3C3.34315 3 2 4.34315 2 6V16C2 17.6569 3.34315 19 5 19H8.34311L10.5858 21.2426C11.3668 22.0237 12.6331 22.0237 13.4142 21.2426L15.6568 19H19C20.6569 19 22 17.6569 22 16V6C22 4.34315 20.6569 3 19 3H5ZM4 6C4 5.44772 4.44772 5 5 5H19C19.5523 5 20 5.44772 20 6V16C20 16.5523 19.5523 17 19 17H14.8284L12 19.8284L9.17154 17H5C4.44772 17 4 16.5523 4 16V6Z"></path></svg></span></span><span class="cdc-suspend-pill__item-number">0</span></button><button class="cdc-icon-btn cdc-suspend-pill__item collect cdc-icon-btn--text" qct-area="收藏文章" qct-click=""><span class="cdc-svg-icon-con"><span class="cdc-svg-icon" style="width:24px;height:24px"><svg width="24" height="24" viewBox="0 0 24 24" fill="currentcolor" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M10.2057 3.11487C10.9393 1.62838 13.059 1.62838 13.7927 3.11487L15.9724 7.53141L20.8463 8.23963C22.4867 8.478 23.1418 10.4939 21.9547 11.651L18.4279 15.0888L19.2605 19.9431C19.5407 21.5769 17.8258 22.8228 16.3586 22.0514L11.9992 19.7596L7.63981 22.0514C6.17255 22.8228 4.45769 21.5769 4.73791 19.9431L5.57048 15.0888L2.04366 11.651C0.856629 10.4939 1.51165 8.478 3.15209 8.23963L8.02603 7.53141L10.2057 3.11487ZM11.9992 4L9.8195 8.41654C9.52818 9.00683 8.96504 9.41597 8.31363 9.51062L3.43969 10.2188L6.9665 13.6566C7.43787 14.1161 7.65297 14.7781 7.5417 15.4269L6.70913 20.2812L11.0685 17.9893C11.6512 17.683 12.3472 17.683 12.9299 17.9893L17.2893 20.2812L16.4567 15.4269C16.3454 14.7781 16.5605 14.1161 17.0319 13.6566L20.5587 10.2188L15.6848 9.51062C15.0333 9.41597 14.4702 9.00683 14.1789 8.41654L11.9992 4Z"></path></svg></span></span><span class="cdc-suspend-pill__item-number">0</span></button><button class="cdc-icon-btn cdc-suspend-pill__item cdc-icon-btn--text"><span class="cdc-svg-icon-con"><span class="cdc-svg-icon" style="width:24px;height:24px"><svg width="24" height="24" viewBox="0 0 24 24" fill="currentcolor" xmlns="http://www.w3.org/2000/svg"><path d="M13.0001 4V6H17.5859L10.1787 13.4072L11.6043 14.81L19.0001 7.41424V12H21.0001V4H13.0001Z"></path><path d="M3 12.9996C3 8.71646 5.99202 5.13211 10 4.22266V6.28952C7.10851 7.15007 5 9.82862 5 12.9996C5 16.8656 8.13401 19.9996 12 19.9996C15.1709 19.9996 17.8494 17.8912 18.71 14.9999H20.7769C19.8674 19.0077 16.2831 21.9996 12 21.9996C7.02944 21.9996 3 17.9702 3 12.9996Z"></path></svg></span></span></button><button class="cdc-icon-btn cdc-suspend-pill__item cdc-icon-btn--text"><span class="cdc-svg-icon-con"><span class="cdc-svg-icon" style="width:24px;height:24px"><svg width="24" height="24" viewBox="0 0 24 24" fill="currentcolor" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M2 6C2 4.34315 3.34315 3 5 3H17C18.6569 3 20 4.34315 20 6V11H18V6C18 5.44772 17.5523 5 17 5H5C4.44772 5 4 5.44772 4 6V18C4 18.5523 4.44772 19 5 19H12V21H5C3.34315 21 2 19.6569 2 18V6ZM6 8H12V10H6V8ZM6 12H15V14H6V12ZM22 16H19V13H17V16H14V18H17V21H19V18H22V16Z"></path></svg></span></span></button><div class="cdc-suspend-pill__line"></div><button class="cdc-icon-btn cdc-suspend-pill__item cdc-icon-btn--text"><span class="cdc-svg-icon-con"><span class="cdc-svg-icon" style="width:24px;height:24px"><svg width="24" height="24" viewBox="0 0 24 24" fill="currentcolor" xmlns="http://www.w3.org/2000/svg"><path d="M16.5047 6H13V4H20V10.876H18V7.33313L14.4571 10.876L13.0429 9.46182L16.5047 6Z"></path><path d="M11 6.00006H7.4953L10.9571 9.46189L9.54291 10.8761L6 7.33319V10.8761H4V4.00006H11V6.00006Z"></path><path d="M7.4953 18.8761H11V20.8761H4V14.0001H6V17.543L9.54291 14.0001L10.9571 15.4143L7.4953 18.8761Z"></path><path d="M16.5047 18.8761H13V20.8761H20V14.0001H18V17.543L14.4571 14.0001L13.0429 15.4143L16.5047 18.8761Z"></path></svg></span></span></button><button class="cdc-icon-btn cdc-suspend-pill__item recommend cdc-icon-btn--text" track-click="{&quot;areaId&quot;:106019,&quot;recPolicyId&quot;:1002,&quot;elementId&quot;:2}" track-exposure="{&quot;areaId&quot;:106019,&quot;recPolicyId&quot;:1002,&quot;elementId&quot;:2}"><span class="cdc-svg-icon-con"><span class="cdc-svg-icon" style="width:24px;height:24px"><svg width="24" height="24" viewBox="0 0 24 24" fill="currentcolor" xmlns="http://www.w3.org/2000/svg"><path d="M5 8H10V10H5V8Z"></path><path d="M10 12H5V14H10V12Z"></path><path d="M14 8H19V10H14V8Z"></path><path d="M19 12H14V14H19V12Z"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M11 20.608L9.57047 20.1996C8.83303 19.9889 8.05701 19.9506 7.30243 20.0878L4.35777 20.6232C3.13009 20.8464 2 19.9033 2 18.6555V5.2669C2 4.2325 2.78877 3.36877 3.81893 3.27512L6.52892 3.02875C7.95704 2.89892 9.39058 3.21084 10.6356 3.9223L12 4.70194L13.3644 3.9223C14.6094 3.21084 16.043 2.89892 17.4711 3.02875L20.1811 3.27512C21.2112 3.36877 22 4.2325 22 5.2669V18.6555C22 19.9033 20.8699 20.8464 19.6422 20.6232L16.6976 20.0878C15.943 19.9506 15.167 19.9889 14.4295 20.1996L13 20.608L12.5 20.8535L12 20.8937L11.5 20.8535L11 20.608ZM6.70999 5.02054C7.73007 4.9278 8.75403 5.1506 9.64336 5.65879L11 6.43401V18.528L10.1199 18.2765C9.0875 17.9815 8.00107 17.928 6.94466 18.1201L4 18.6555V5.2669L6.70999 5.02054ZM13 18.528L13.8801 18.2765C14.9125 17.9815 15.9989 17.928 17.0553 18.1201L20 18.6555V5.2669L17.29 5.02054C16.2699 4.9278 15.246 5.1506 14.3566 5.65879L13 6.43401V18.528Z"></path></svg></span></span><span class="cdc-suspend-pill__item-text">推荐</span></button></div></div></div></div></div><script> if (!String.prototype.replaceAll) { String.prototype.replaceAll = function (str, newStr) { // If a regex pattern if (Object.prototype.toString.call(str).toLowerCase() === '[object regexp]') { return this.replace(str, newStr); } // If a string return this.replace(new RegExp(str, 'g'), newStr); }; } </script><script src="https://developer.qcloudimg.com/static/jquery.min.js"></script><script src="https://cloud.tencent.com/qccomponent/login/api.js"></script><script src="https://cloudcache.tencent-cloud.com/qcloud/main/scripts/release/common/vendors/react/react.16.8.6.min.js"></script><script src="https://web.sdk.qcloud.com/player/tcplayer/release/v4.5.1/libs/TXLivePlayer-1.2.0.min.js" defer=""></script><script src="https://cloudcache.tencent-cloud.com/open/qcloud/video/tcplayer/libs/hls.min.0.13.2m.js"></script><script src="https://cloudcache.tencent-cloud.com/open/qcloud/video/tcplayer/tcplayer.v4.1.min.js"></script><script id="__NEXT_DATA__" type="application/json">{"props":{"isMobile":false,"isSupportWebp":false,"currentDomain":"cloud.tencent.com","baseUrl":"https://cloud.tencent.com","reqId":"NJoiE2wksNiASnBpxZAc4","query":{"policyId":"20240000","traceId":"","articleId":"2471045"},"platform":"other","env":"production","__N_SSP":true,"pageProps":{"fallback":{"#url:\"/api/article/detail\",params:#articleId:2471045,,":{"articleData":{"articleId":2471045,"codeLineNum":50,"readingTime":726,"wordsNum":3327},"articleInfo":{"articleId":2471045,"channel":0,"commentNum":0,"content":{"blocks":[{"key":"6l3cs","text":"","type":"atomic","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{"theme":"theme-orange"}},{"key":"88u34","text":"Hey小伙伴们,今天要给大家安利一篇操作实践的文章,相信大家通过仔细阅读,一定会有所收货!","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}},{"key":"1jq58","text":"🔗【文章链接】:『前端必修课』按钮边框的旋转动画","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[{"offset":8,"length":16,"key":0}],"data":{}},{"key":"6ghg7","text":"💖 推荐理由:这篇文章是由 BNTang 撰写的,这篇文章教读者如何使用 CSS 实现旋转按钮边框效果。知识点包括使用 :before 和 :after 元素简化 HTML 结构,结合 transform 创建动画效果,通过 transform-origin 精确控制旋转中心。通过这些方法,够掌握动态效果的实现,提升前端动画设计能力,应用于更复杂的视觉效果中。","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":60,"length":7,"style":"CODE"},{"offset":70,"length":6,"style":"CODE"},{"offset":93,"length":9,"style":"CODE"},{"offset":113,"length":16,"style":"CODE"}],"entityRanges":[{"offset":14,"length":7,"key":1}],"data":{}},{"key":"c7fj","text":"引言","type":"header-one","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{"text":"%E5%BC%95%E8%A8%80"}},{"key":"2sq2s","text":"在 Java 编程语言中,数据类型的使用至关重要。作为一种静态类型语言,Java 提供了丰富的基本数据类型和对应的包装类。其中,Integer 类是 int 类型的包装类,承载着更复杂的功能,如缓存、装箱和拆箱等。掌握 Integer 类的工作原理,尤其是其装箱和拆箱机制,不仅可以提升程序性能,还能在面试中展示出对 Java 深厚的理解。","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":64,"length":7,"style":"CODE"},{"offset":75,"length":3,"style":"CODE"},{"offset":110,"length":7,"style":"CODE"}],"entityRanges":[],"data":{}},{"key":"5ca1u","text":"本文将深入探讨 Integer 类的特性、装箱和拆箱的性能影响、常见的面试问题以及在不同版本中的变化,力求为读者提供全面的知识体系。","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":8,"length":7,"style":"CODE"}],"entityRanges":[],"data":{}},{"key":"dv56d","text":"图片","type":"atomic","depth":0,"inlineStyleRanges":[],"entityRanges":[{"offset":0,"length":2,"key":2}],"data":{}},{"key":"7hadk","text":"Integer 类概述","type":"header-one","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{"text":"Integer-%E7%B1%BB%E6%A6%82%E8%BF%B0"}},{"key":"5h7nc","text":"Integer 类是 Java 中最常用的包装类之一,提供的一个用于表示整数的类。它实现了 Comparable 接口,允许 Integer 对象进行排序。Integer 类还提供了一些常用的方法,如转换、比较和常量定义。","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}},{"key":"b5vl5","text":"范围:Integer 类型的有效范围为 -2,147,483,648 到 2,147,483,647。","type":"unordered-list-item","depth":0,"inlineStyleRanges":[{"offset":0,"length":2,"style":"BOLD"}],"entityRanges":[],"data":{}},{"key":"tmm2","text":"不可变性:Integer 对象一旦创建,其值不能更改。","type":"unordered-list-item","depth":0,"inlineStyleRanges":[{"offset":0,"length":4,"style":"BOLD"}],"entityRanges":[],"data":{}},{"key":"4mg2k","text":"常用方法:","type":"unordered-list-item","depth":0,"inlineStyleRanges":[{"offset":0,"length":4,"style":"BOLD"}],"entityRanges":[],"data":{}},{"key":"5j0fm","text":"compareTo(): 比较两个 Integer 对象的大小。","type":"unordered-list-item","depth":0,"inlineStyleRanges":[{"offset":0,"length":11,"style":"CODE"},{"offset":18,"length":7,"style":"CODE"}],"entityRanges":[],"data":{}},{"key":"59uga","text":"valueOf(): 返回 int 的 Integer 对象,使用缓存机制。","type":"unordered-list-item","depth":0,"inlineStyleRanges":[{"offset":0,"length":9,"style":"CODE"},{"offset":14,"length":3,"style":"CODE"},{"offset":20,"length":7,"style":"CODE"}],"entityRanges":[],"data":{}},{"key":"comj9","text":"parseInt(): 将字符串转换为 int 类型。","type":"unordered-list-item","depth":0,"inlineStyleRanges":[{"offset":0,"length":10,"style":"CODE"},{"offset":20,"length":3,"style":"CODE"}],"entityRanges":[],"data":{}},{"key":"8e55t","text":"Java 提供了自动装箱和拆箱的功能,使得基本类型与其对应的对象类型之间的转换变得更加简单和直观。","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}},{"key":"5mrlj","text":"装箱与拆箱","type":"header-two","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{"text":"%E8%A3%85%E7%AE%B1%E4%B8%8E%E6%8B%86%E7%AE%B1"}},{"key":"9rn3a","text":"装箱(Boxing):将基本数据类型转换为其对应的对象类型。自动装箱使得开发者无需显式地创建 Integer 对象。","type":"unordered-list-item","depth":0,"inlineStyleRanges":[{"offset":0,"length":2,"style":"BOLD"},{"offset":47,"length":7,"style":"CODE"}],"entityRanges":[],"data":{}},{"key":"3bj3b","text":"Integer boxed = 10; // 自动装箱","type":"code-block","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{"syntax":"java"}},{"key":"6hs0f","text":"拆箱(Unboxing):将对象类型转换为基本数据类型。拆箱在使用 Integer 对象时会自动发生。","type":"unordered-list-item","depth":0,"inlineStyleRanges":[{"offset":0,"length":2,"style":"BOLD"},{"offset":34,"length":7,"style":"CODE"}],"entityRanges":[],"data":{}},{"key":"tafo","text":"int primitive = boxed; // 拆箱","type":"code-block","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{"syntax":"java"}},{"key":"cakmh","text":"这两个过程虽然提供了便利,但也伴随着性能开销。","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}},{"key":"fan9q","text":"装箱与拆箱的性能影响","type":"header-one","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{"text":"%E8%A3%85%E7%AE%B1%E4%B8%8E%E6%8B%86%E7%AE%B1%E7%9A%84%E6%80%A7%E8%83%BD%E5%BD%B1%E5%93%8D"}},{"key":"7cje2","text":"性能开销分析","type":"header-two","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{"text":"%E6%80%A7%E8%83%BD%E5%BC%80%E9%94%80%E5%88%86%E6%9E%90"}},{"key":"flbv0","text":"装箱的开销","type":"header-three","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{"text":"%E8%A3%85%E7%AE%B1%E7%9A%84%E5%BC%80%E9%94%80"}},{"key":"dna42","text":"装箱涉及到内存分配和对象初始化。在频繁的操作中,例如在循环内进行装箱,可能会显著影响性能。在这个例子中,Integer.valueOf(i) 会尝试返回缓存中的对象,但对于大于 127 的值,依然会创建新对象,导致额外的性能开销。","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}},{"key":"9o1vn","text":"for (int i = 0; i \u003c 1000000; i++) {\n Integer boxed = Integer.valueOf(i); // 每次都会创建新对象\n}","type":"code-block","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{"syntax":"java"}},{"key":"2i3u2","text":"拆箱的开销","type":"header-three","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{"text":"%E6%8B%86%E7%AE%B1%E7%9A%84%E5%BC%80%E9%94%80"}},{"key":"c7iuc","text":"拆箱虽然相对开销小,但在大规模数据处理时,多个拆箱操作可能会导致性能问题。在这种情况下,虽然代码简洁,但会产生大量的拆箱操作。","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}},{"key":"4lmpd","text":"List\u003cInteger\u003e list = new ArrayList\u003c\u003e();\nfor (int i = 0; i \u003c 1000000; i++) {\n list.add(i); // 自动装箱\n}\n\nfor (Integer boxed : list) {\n int primitive = boxed; // 拆箱,转换为基本类型\n}","type":"code-block","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{"syntax":"java"}},{"key":"dsi5o","text":"性能优化策略","type":"header-two","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{"text":"%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96%E7%AD%96%E7%95%A5"}},{"key":"1b0j7","text":"为了提高性能,可以采取以下措施:","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}},{"key":"8rvs0","text":"使用基本类型:在性能要求高的场景下,尽量使用基本类型而非包装类。","type":"unordered-list-item","depth":0,"inlineStyleRanges":[{"offset":0,"length":6,"style":"BOLD"}],"entityRanges":[],"data":{}},{"key":"o9bt","text":"使用数组:使用基本类型数组而非 List\u003cInteger\u003e,可以避免装箱和拆箱带来的开销。","type":"unordered-list-item","depth":0,"inlineStyleRanges":[{"offset":0,"length":4,"style":"BOLD"},{"offset":16,"length":13,"style":"CODE"}],"entityRanges":[],"data":{}},{"key":"9km7c","text":"合理使用集合:在处理大量数据时,可以考虑使用 IntStream 等流式操作,减少装箱和拆箱的频率。","type":"unordered-list-item","depth":0,"inlineStyleRanges":[{"offset":0,"length":6,"style":"BOLD"}],"entityRanges":[],"data":{}},{"key":"lrf","text":"根据实际情况进行性能测试,比较使用基本类型和包装类的性能。例如,在处理大量数据时,使用 int[] 数组比使用 ArrayList\u003cInteger\u003e 更高效。","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":44,"length":5,"style":"CODE"},{"offset":56,"length":18,"style":"CODE"}],"entityRanges":[],"data":{}},{"key":"4oga1","text":"long startTime = System.nanoTime();\nfor (int i = 0; i \u003c 1000000; i++) {\n Integer boxed = Integer.valueOf(i); // 装箱\n}\nlong endTime = System.nanoTime();\nSystem.out.println(\"Boxing time: \" + (endTime - startTime) + \" ns\");\n\nstartTime = System.nanoTime();\nfor (int i = 0; i \u003c 1000000; i++) {\n int primitive = i; // 直接使用基本类型\n}\nendTime = System.nanoTime();\nSystem.out.println(\"Primitive time: \" + (endTime - startTime) + \" ns\");","type":"code-block","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{"syntax":"java"}},{"key":"6p2dm","text":"图片","type":"atomic","depth":0,"inlineStyleRanges":[],"entityRanges":[{"offset":0,"length":2,"key":3}],"data":{}},{"key":"7bls3","text":"常见面试问题","type":"header-one","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{"text":"%E5%B8%B8%E8%A7%81%E9%9D%A2%E8%AF%95%E9%97%AE%E9%A2%98"}},{"key":"99avp","text":"面试问题示例","type":"header-three","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{"text":"%E9%9D%A2%E8%AF%95%E9%97%AE%E9%A2%98%E7%A4%BA%E4%BE%8B"}},{"key":"bpmtm","text":"装箱和拆箱的性能开销是什么?","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":0,"length":14,"style":"BOLD"}],"entityRanges":[],"data":{}},{"key":"do5gb","text":"答案:装箱和拆箱会产生内存分配和对象创建的开销。在进行装箱时,Java 需要为每个基本类型创建一个新的对象,这可能会导致频繁的内存分配和垃圾回收。在高频率的操作中,特别是在循环中,这种开销会显著影响性能,导致应用程序的响应变慢。","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":0,"length":2,"style":"BOLD"}],"entityRanges":[],"data":{}},{"key":"di10","text":"在 Java 中,如何优化装箱和拆箱的性能?","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":0,"length":22,"style":"BOLD"}],"entityRanges":[],"data":{}},{"key":"53evd","text":"答案:在性能关键的代码中,应优先使用基本类型而非包装类。例如,使用 int 而不是 Integer。在使用集合时,可以考虑使用原始类型数组(如 int[])或其他高效的数据结构,避免不必要的装箱和拆箱操作。此外,如果必须使用集合,选择合适的容器类型,尽量减少装箱的发生。","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":0,"length":2,"style":"BOLD"},{"offset":34,"length":3,"style":"CODE"},{"offset":42,"length":7,"style":"CODE"},{"offset":72,"length":5,"style":"CODE"}],"entityRanges":[],"data":{}},{"key":"1uoo0","text":"什么情况下会使用装箱和拆箱?","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":0,"length":14,"style":"BOLD"}],"entityRanges":[],"data":{}},{"key":"4cp0s","text":"答案:装箱和拆箱通常在以下情况下使用:","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":0,"length":2,"style":"BOLD"}],"entityRanges":[],"data":{}},{"key":"59vl1","text":"当需要使用对象类型时,例如在集合(如 ArrayList\u003cInteger\u003e)中存储元素,Java 会自动进行装箱。","type":"unordered-list-item","depth":0,"inlineStyleRanges":[{"offset":19,"length":18,"style":"CODE"}],"entityRanges":[],"data":{}},{"key":"a84pr","text":"在使用泛型类型时,基本类型无法直接用于泛型,Java 需要进行装箱以创建对应的对象类型。","type":"unordered-list-item","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}},{"key":"3rijj","text":"在 API 方法中,如果方法参数是对象类型,而实际传入的是基本类型,Java 会自动进行装箱。","type":"unordered-list-item","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}},{"key":"fee47","text":"缓存机制","type":"header-one","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{"text":"%E7%BC%93%E5%AD%98%E6%9C%BA%E5%88%B6"}},{"key":"2eqi5","text":"Java 中的 Integer 类实现了对象缓存机制。这意味着对于 -128 到 127 之间的整数值,Java 会在内存中缓存这些对象,确保同一个值的 Integer 对象是相同的引用。","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":8,"length":7,"style":"CODE"},{"offset":77,"length":7,"style":"CODE"}],"entityRanges":[],"data":{}},{"key":"6f467","text":"为什么选择 -128 到 127 的范围","type":"header-two","depth":0,"inlineStyleRanges":[{"offset":0,"length":20,"style":"BOLD"}],"entityRanges":[],"data":{"text":"%E4%B8%BA%E4%BB%80%E4%B9%88%E9%80%89%E6%8B%A9--128-%E5%88%B0-127-%E7%9A%84%E8%8C%83%E5%9B%B4"}},{"key":"2hn4o","text":"这个范围的选择是为了在常用的计算中优化内存使用。因为在许多情况下,小整数会被频繁使用,缓存可以显著提高性能,减少内存分配和垃圾回收的压力。","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}},{"key":"5fndo","text":"自动装箱和手动装箱","type":"header-two","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{"text":"%E8%87%AA%E5%8A%A8%E8%A3%85%E7%AE%B1%E5%92%8C%E6%89%8B%E5%8A%A8%E8%A3%85%E7%AE%B1"}},{"key":"as16f","text":"当我们将 int 赋值给 Integer 对象时,Java 会自动进行装箱:","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":5,"length":3,"style":"CODE"},{"offset":13,"length":7,"style":"CODE"}],"entityRanges":[],"data":{}},{"key":"5c1kn","text":"Integer a = 10; // 自动装箱","type":"code-block","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{"syntax":"java"}},{"key":"b6h60","text":"而手动装箱则是使用 Integer 类的构造函数或静态方法:","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":10,"length":7,"style":"CODE"}],"entityRanges":[],"data":{}},{"key":"ekokk","text":"Integer b = Integer.valueOf(10); // 手动装箱","type":"code-block","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{"syntax":"java"}},{"key":"bfu7n","text":"在性能上,自动装箱会增加一些开销,但在多数情况下可以忽略。","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}},{"key":"8cur5","text":"比较 Integer 对象的方式","type":"header-two","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{"text":"%E6%AF%94%E8%BE%83-Integer-%E5%AF%B9%E8%B1%A1%E7%9A%84%E6%96%B9%E5%BC%8F"}},{"key":"durj3","text":"比较 Integer 对象时,常见的错误在于使用 == 操作符。使用 == 时,比较的是对象的引用:","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":3,"length":7,"style":"CODE"},{"offset":25,"length":2,"style":"CODE"},{"offset":35,"length":2,"style":"CODE"}],"entityRanges":[],"data":{}},{"key":"80ei","text":"Integer x = 127;\nInteger y = 127;\nSystem.out.println(x == y); // true\n\nInteger z = 128;\nInteger w = 128;\nSystem.out.println(z == w); // false","type":"code-block","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{"syntax":"java"}},{"key":"2d123","text":"为了比较值,应始终使用 equals() 方法:","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":12,"length":8,"style":"CODE"}],"entityRanges":[],"data":{}},{"key":"eglld","text":"System.out.println(z.equals(w)); // true","type":"code-block","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{"syntax":"java"}},{"key":"7fqt1","text":"源码分析","type":"header-two","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{"text":"%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90"}},{"key":"661hr","text":"在 Integer 类中,Integer.valueOf() 方法负责返回指定值的 Integer 对象。对于 -128 到 127 的值,它会返回缓存中的对象;对于其他值,则会新建对象。","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":2,"length":7,"style":"CODE"},{"offset":13,"length":17,"style":"CODE"},{"offset":42,"length":7,"style":"CODE"}],"entityRanges":[],"data":{}},{"key":"320hu","text":"关键源码片段:","type":"header-three","depth":0,"inlineStyleRanges":[{"offset":0,"length":6,"style":"BOLD"}],"entityRanges":[],"data":{"text":"%E5%85%B3%E9%94%AE%E6%BA%90%E7%A0%81%E7%89%87%E6%AE%B5%EF%BC%9A"}},{"key":"4tf0i","text":"图片","type":"atomic","depth":0,"inlineStyleRanges":[],"entityRanges":[{"offset":0,"length":2,"key":4}],"data":{}},{"key":"5ff1e","text":"大致意思:","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}},{"key":"afrm9","text":"public static Integer valueOf(int i) {\n if (i \u003e= -128 \u0026\u0026 i \u003c= 127) {\n return IntegerCache.cache[i + 128];\n }\n return new Integer(i);\n}","type":"code-block","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{"syntax":"java"}},{"key":"6m9c9","text":"IntegerCache 实现:","type":"header-three","depth":0,"inlineStyleRanges":[{"offset":0,"length":12,"style":"BOLD"},{"offset":13,"length":2,"style":"BOLD"},{"offset":0,"length":12,"style":"CODE"}],"entityRanges":[],"data":{"text":"IntegerCache-%E5%AE%9E%E7%8E%B0%EF%BC%9A"}},{"key":"birl","text":"图片","type":"atomic","depth":0,"inlineStyleRanges":[],"entityRanges":[{"offset":0,"length":2,"key":5}],"data":{}},{"key":"5us4k","text":"大致意思:","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}},{"key":"2k49s","text":"static final class IntegerCache {\n static final Integer cache[] = new Integer[-(-128) + 127 + 1];\n static {\n for (int i = 0; i \u003c cache.length; i++) {\n cache[i] = new Integer(i - 128);\n }\n }\n}","type":"code-block","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{"syntax":"java"}},{"key":"5cb0k","text":"面试场景中的应用","type":"header-one","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{"text":"%E9%9D%A2%E8%AF%95%E5%9C%BA%E6%99%AF%E4%B8%AD%E7%9A%84%E5%BA%94%E7%94%A8"}},{"key":"eb4i9","text":"在面试中,考官可能会询问有关 Integer 类的问题,例如:","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":15,"length":7,"style":"CODE"}],"entityRanges":[],"data":{}},{"key":"er87g","text":"“Java 中 Integer 的缓存机制是如何工作的?”","type":"blockquote","depth":0,"inlineStyleRanges":[{"offset":0,"length":29,"style":"BOLD"},{"offset":8,"length":7,"style":"CODE"}],"entityRanges":[],"data":{}},{"key":"26bpc","text":"在 Java 中,Integer 类的缓存机制是通过静态内部类 IntegerCache 实现的。Java 会缓存范围在 -128 到 127 之间的 Integer 对象,这意味着在这个范围内,任何相同的整数值都会引用相同的 Integer 对象。这个机制的目的是为了优化性能,减少对象的创建和内存的使用。","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}},{"key":"75slt","text":"当你使用 Integer.valueOf(int i) 方法时,如果 i 在缓存范围内,方法会直接返回缓存的对象;如果不在范围内,则会创建新的 Integer 对象。这种设计确保了对于常用的小整数的高效处理,避免了不必要的内存分配。","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}},{"key":"afqr3","text":"“在 Java 中,使用 == 和 equals() 的区别是什么?”","type":"blockquote","depth":0,"inlineStyleRanges":[{"offset":0,"length":35,"style":"BOLD"},{"offset":13,"length":2,"style":"CODE"},{"offset":18,"length":8,"style":"CODE"}],"entityRanges":[],"data":{}},{"key":"1nst3","text":"在 Java 中,== 操作符用于比较两个对象的引用,即它们是否指向同一个内存地址。而 equals() 方法用于比较两个对象的实际内容(值)。","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}},{"key":"2t9ht","text":"对于 Integer 类型:","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}},{"key":"4offq","text":"当你使用 == 比较两个 Integer 对象时,如果它们在缓存范围内(-128 到 127),将返回 true,因为它们引用相同的对象;如果超出范围,可能会返回 false,因为它们是不同的对象。","type":"unordered-list-item","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}},{"key":"3bka4","text":"使用 equals() 方法始终比较两个对象的值,因此即使两个 Integer 对象是不同的实例,只要它们的值相同,equals() 方法都会返回 true。","type":"unordered-list-item","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}},{"key":"chdtj","text":"总结","type":"header-one","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{"text":"%E6%80%BB%E7%BB%93"}},{"key":"883pj","text":"在深入探讨 Java 中 Integer 类的特性、装箱与拆箱的性能影响以及相关的最佳实践后,我们可以清晰地认识到,合理使用基本数据类型和包装类对性能优化至关重要。了解 Integer 的缓存机制和比较方式,使我们能够在开发中避免潜在的错误和性能瓶颈。","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":13,"length":7,"style":"CODE"},{"offset":85,"length":7,"style":"CODE"}],"entityRanges":[],"data":{}},{"key":"e3tur","text":"对于面试者来说,掌握这些概念不仅能帮助回答相关问题,更能展示对 Java 语言的深入理解。在实际开发中,灵活应用这些知识将显著提升代码的效率和可维护性。","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}},{"key":"95kt8","text":"随着 Java 语言的不断演进,理解这些基本概念将为未来的技术挑战打下坚实基础。希望本文能够为大家提供有价值的见解,帮助在日常编程和职业发展中取得成功。","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}}],"entityMap":{"0":{"type":"LINK","mutability":"MUTABLE","data":{"url":"https://cloud.tencent.com/developer/article/2470413"}},"1":{"type":"LINK","mutability":"MUTABLE","data":{"url":"https://cloud.tencent.com/developer/user/7271764"}},"2":{"type":"IMAGE","mutability":"IMMUTABLE","data":{"placeHolderUrl":null,"error":null,"dpi":0,"imageUrl":"https://developer.qcloudimg.com/http-save/yehe-5421023/d8aa98a8ca8ea0dc6c63a7a66b09e4c0.png","blockWidth":700,"blockHeight":400}},"3":{"type":"IMAGE","mutability":"IMMUTABLE","data":{"placeHolderUrl":null,"error":null,"dpi":0,"imageUrl":"https://developer.qcloudimg.com/http-save/yehe-5421023/a7330b259e362e8aa06c4e9061643047.png","blockWidth":766,"blockHeight":350}},"4":{"type":"IMAGE","mutability":"IMMUTABLE","data":{"name":null,"imageUrl":"https://developer.qcloudimg.com/http-save/yehe-5421023/9afe2ef0e09d344d65f921716ab753bc.png","blockWidth":766,"blockHeight":518}},"5":{"type":"IMAGE","mutability":"IMMUTABLE","data":{"name":null,"blockWidth":766,"blockHeight":407,"imageUrl":"https://developer.qcloudimg.com/http-save/yehe-5421023/9d3187688b7f40146ccd73a43fa97eb7.png"}}}},"createTime":1732584067,"ext":{"closeTextLink":0,"comment_ban":0,"description":"","focusRead":0},"favNum":0,"isOriginal":0,"likeNum":4,"pic":"https://developer.qcloudimg.com/http-save/yehe-5421023/b10c377301ee7135fa90c79240dcf545.png","plain":"Hey小伙伴们,今天要给大家安利一篇操作实践的文章,相信大家通过仔细阅读,一定会有所收货!🔗【文章链接】:『前端必修课』按钮边框的旋转动画💖 推荐理由:这篇文章是由 BNTang 撰写的,这篇文章教读者如何使用 CSS 实现旋转按钮边框效果。知识点包括使用 :before 和 :after 元素简化 HTML 结构,结合 transform 创建动画效果,通过 transform-origin 精确控制旋转中心。通过这些方法,够掌握动态效果的实现,提升前端动画设计能力,应用于更复杂的视觉效果中。引言在 Java 编程语言中,数据类型的使用至关重要。作为一种静态类型语言,Java 提供了丰富的基本数据类型和对应的包装类。其中,Integer 类是 int 类型的包装类,承载着更复杂的功能,如缓存、装箱和拆箱等。掌握 Integer 类的工作原理,尤其是其装箱和拆箱机制,不仅可以提升程序性能,还能在面试中展示出对 Java 深厚的理解。本文将深入探讨 Integer 类的特性、装箱和拆箱的性能影响、常见的面试问题以及在不同版本中的变化,力求为读者提供全面的知识体系。Integer 类概述Integer 类是 Java 中最常用的包装类之一,提供的一个用于表示整数的类。它实现了 Comparable 接口,允许 Integer 对象进行排序。Integer 类还提供了一些常用的方法,如转换、比较和常量定义。范围:Integer 类型的有效范围为 -2,147,483,648 到 2,147,483,647。不可变性:Integer 对象一旦创建,其值不能更改。常用方法:compareTo(): 比较两个 Integer 对象的大小。valueOf(): 返回 int 的 Integer 对象,使用缓存机制。parseInt(): 将字符串转换为 int 类型。Java 提供了自动装箱和拆箱的功能,使得基本类型与其对应的对象类型之间的转换变得更加简单和直观。装箱与拆箱装箱(Boxing):将基本数据类型转换为其对应的对象类型。自动装箱使得开发者无需显式地创建 Integer 对象。Integer boxed = 10; // 自动装箱拆箱(Unboxing):将对象类型转换为基本数据类型。拆箱在使用 Integer 对象时会自动发生。int primitive = boxed; // 拆箱这两个过程虽然提供了便利,但也伴随着性能开销。装箱与拆箱的性能影响性能开销分析装箱的开销装箱涉及到内存分配和对象初始化。在频繁的操作中,例如在循环内进行装箱,可能会显著影响性能。在这个例子中,Integer.valueOf(i) 会尝试返回缓存中的对象,但对于大于 127 的值,依然会创建新对象,导致额外的性能开销。for (int i = 0; i \u003c 1000000; i++) { Integer boxed = Integer.valueOf(i); // 每次都会创建新对象}拆箱的开销拆箱虽然相对开销小,但在大规模数据处理时,多个拆箱操作可能会导致性能问题。在这种情况下,虽然代码简洁,但会产生大量的拆箱操作。List\u003cInteger\u003e list = new ArrayList\u003c\u003e();for (int i = 0; i \u003c 1000000; i++) { list.add(i); // 自动装箱}for (Integer boxed : list) { int primitive = boxed; // 拆箱,转换为基本类型}性能优化策略为了提高性能,可以采取以下措施:使用基本类型:在性能要求高的场景下,尽量使用基本类型而非包装类。使用数组:使用基本类型数组而非 List\u003cInteger\u003e,可以避免装箱和拆箱带来的开销。合理使用集合:在处理大量数据时,可以考虑使用 IntStream 等流式操作,减少装箱和拆箱的频率。根据实际情况进行性能测试,比较使用基本类型和包装类的性能。例如,在处理大量数据时,使用 int[] 数组比使用 ArrayList\u003cInteger\u003e 更高效。long startTime = System.nanoTime();for (int i = 0; i \u003c 1000000; i++) { Integer boxed = Integer.valueOf(i); // 装箱}long endTime = System.nanoTime();System.out.println(\"Boxing time: \" + (endTime - startTime) + \" ns\");startTime = System.nanoTime();for (int i = 0; i \u003c 1000000; i++) { int primitive = i; // 直接使用基本类型}endTime = System.nanoTime();System.out.println(\"Primitive time: \" + (endTime - startTime) + \" ns\");常见面试问题面试问题示例装箱和拆箱的性能开销是什么?答案:装箱和拆箱会产生内存分配和对象创建的开销。在进行装箱时,Java 需要为每个基本类型创建一个新的对象,这可能会导致频繁的内存分配和垃圾回收。在高频率的操作中,特别是在循环中,这种开销会显著影响性能,导致应用程序的响应变慢。在 Java 中,如何优化装箱和拆箱的性能?答案:在性能关键的代码中,应优先使用基本类型而非包装类。例如,使用 int 而不是 Integer。在使用集合时,可以考虑使用原始类型数组(如 int[])或其他高效的数据结构,避免不必要的装箱和拆箱操作。此外,如果必须使用集合,选择合适的容器类型,尽量减少装箱的发生。什么情况下会使用装箱和拆箱?答案:装箱和拆箱通常在以下情况下使用:当需要使用对象类型时,例如在集合(如 ArrayList\u003cInteger\u003e)中存储元素,Java 会自动进行装箱。在使用泛型类型时,基本类型无法直接用于泛型,Java 需要进行装箱以创建对应的对象类型。在 API 方法中,如果方法参数是对象类型,而实际传入的是基本类型,Java 会自动进行装箱。缓存机制Java 中的 Integer 类实现了对象缓存机制。这意味着对于 -128 到 127 之间的整数值,Java 会在内存中缓存这些对象,确保同一个值的 Integer 对象是相同的引用。为什么选择 -128 到 127 的范围这个范围的选择是为了在常用的计算中优化内存使用。因为在许多情况下,小整数会被频繁使用,缓存可以显著提高性能,减少内存分配和垃圾回收的压力。自动装箱和手动装箱当我们将 int 赋值给 Integer 对象时,Java 会自动进行装箱:Integer a = 10; // 自动装箱而手动装箱则是使用 Integer 类的构造函数或静态方法:Integer b = Integer.valueOf(10); // 手动装箱在性能上,自动装箱会增加一些开销,但在多数情况下可以忽略。比较 Integer 对象的方式比较 Integer 对象时,常见的错误在于使用 == 操作符。使用 == 时,比较的是对象的引用:Integer x = 127;Integer y = 127;System.out.println(x == y); // trueInteger z = 128;Integer w = 128;System.out.println(z == w); // false为了比较值,应始终使用 equals() 方法:System.out.println(z.equals(w)); // true源码分析在 Integer 类中,Integer.valueOf() 方法负责返回指定值的 Integer 对象。对于 -128 到 127 的值,它会返回缓存中的对象;对于其他值,则会新建对象。关键源码片段:大致意思:public static Integer valueOf(int i) { if (i \u003e= -128 \u0026\u0026 i \u003c= 127) { return IntegerCache.cache[i + 128]; } return new Integer(i);}IntegerCache 实现:大致意思:static final class IntegerCache { static final Integer cache[] = new Integer[-(-128) + 127 + 1]; static { for (int i = 0; i \u003c cache.length; i++) { cache[i] = new Integer(i - 128); } }}面试场景中的应用在面试中,考官可能会询问有关 Integer 类的问题,例如:“Java 中 Integer 的缓存机制是如何工作的?”在 Java 中,Integer 类的缓存机制是通过静态内部类 IntegerCache 实现的。Java 会缓存范围在 -128 到 127 之间的 Integer 对象,这意味着在这个范围内,任何相同的整数值都会引用相同的 Integer 对象。这个机制的目的是为了优化性能,减少对象的创建和内存的使用。当你使用 Integer.valueOf(int i) 方法时,如果 i 在缓存范围内,方法会直接返回缓存的对象;如果不在范围内,则会创建新的 Integer 对象。这种设计确保了对于常用的小整数的高效处理,避免了不必要的内存分配。“在 Java 中,使用 == 和 equals() 的区别是什么?”在 Java 中,== 操作符用于比较两个对象的引用,即它们是否指向同一个内存地址。而 equals() 方法用于比较两个对象的实际内容(值)。对于 Integer 类型:当你使用 == 比较两个 Integer 对象时,如果它们在缓存范围内(-128 到 127),将返回 true,因为它们引用相同的对象;如果超出范围,可能会返回 false,因为它们是不同的对象。使用 equals() 方法始终比较两个对象的值,因此即使两个 Integer 对象是不同的实例,只要它们的值相同,equals() 方法都会返回 true。总结在深入探讨 Java 中 Integer 类的特性、装箱与拆箱的性能影响以及相关的最佳实践后,我们可以清晰地认识到,合理使用基本数据类型和包装类对性能优化至关重要。了解 Integer 的缓存机制和比较方式,使我们能够在开发中避免潜在的错误和性能瓶颈。对于面试者来说,掌握这些概念不仅能帮助回答相关问题,更能展示对 Java 语言的深入理解。在实际开发中,灵活应用这些知识将显著提升代码的效率和可维护性。随着 Java 语言的不断演进,理解这些基本概念将为未来的技术挑战打下坚实基础。希望本文能够为大家提供有价值的见解,帮助在日常编程和职业发展中取得成功。","showReadNum":183,"sourceDetail":null,"sourceType":1,"status":2,"summary":"Hey小伙伴们,今天要给大家安利一篇操作实践的文章,相信大家通过仔细阅读,一定会有所收货!","tagIds":[18094],"title":"面试真题:Integer(128)引发的思考","uid":5421023,"updateTime":1732584101,"userSummary":"","userUpdateTime":1732584067,"isNewArticle":false},"authorInfo":{"articleNum":0,"avatarUrl":"https://developer.qcloudimg.com/http-save/10011/32d13fd8aae684f33b8b8f8bbc65fdca.jpg","company":"大连东芯科技有限公司","introduce":"须知少日拏云志,曾许人间第一流","isProfessionVerified":1,"nickname":"不惑","privilege":1,"title":"资深软件工程师","uid":5421023},"authorType":{"isBlogMoveAuthor":0,"isCoCreator":1,"isInternalAuthor":0,"isOriginalAuthor":1},"classify":[{"id":5,"name":"编程语言"}],"columnInfo":{"columnAvatar":"https://cloudcache.tencent-cloud.com/qcloud/developer/images/release/column-icons/3.png","columnDesc":"面试经验系列,汇集了众多成功与失败的案例。通过总结这些经验教训,从他人的经历中汲取宝贵的精华,不断完善自身的不足。只有做到知己知彼,才能在求职过程中百战不殆。","columnId":103678,"columnName":"面经","createTime":1725260032,"createUid":5421023,"memberNum":1,"showArticleNum":21,"showConcernNum":13},"columnList":[{"columnAvatar":"https://cloudcache.tencent-cloud.com/qcloud/developer/images/release/column-icons/3.png","columnDesc":"面试经验系列,汇集了众多成功与失败的案例。通过总结这些经验教训,从他人的经历中汲取宝贵的精华,不断完善自身的不足。只有做到知己知彼,才能在求职过程中百战不殆。","columnId":103678,"columnName":"面经","createTime":1725260032,"createUid":5421023,"memberNum":1,"showArticleNum":21,"showConcernNum":13}],"editTime":0,"isTencent":false,"longtailTags":["Integer"],"publishTime":1732584067,"sourceDetail":{"blogType":0,"blogUrl":"","channelSource":"","originalTime":"","sourceAuthor":"","sourceLink":"","wechatNickName":"","wechatUserName":""},"tags":[{"categoryId":99,"createTime":"2024-11-08T16:19:13+08:00","groupId":0,"groupName":"","tagId":18094,"tagName":"腾讯技术创作特训营S10"}],"textLink":[{"ext":{"categoryId":1033,"categoryName":"通用技术 - 游戏开发","desc":"数据结构是计算机科学中研究数据组织和存储方式的一门学科,主要研究各种数据对象在计算机内存中的组织方式和存储结构,以及对这些数据对象进行操作的算法和方法。在计算机程序设计中,数据结构是指数据元素之间的关系和操作方式的抽象描述,是程序设计中最基本的概念之一。","kpCount":7,"name":"数据结构","pCategoryId":1002,"termId":1914},"id":3612,"link":"https://cloud.tencent.com/developer/techpedia/1914","sources":[2],"text":"数据结构"},{"ext":{"categoryId":1029,"categoryName":"通用技术 - 测试","desc":"性能测试是软件测试中的一种测试类型,主要用于评估软件系统在各种负载条件下的性能、稳定性、可靠性和可扩展性等方面。","kpCount":12,"name":"性能测试","pCategoryId":1002,"termId":1929},"id":3627,"link":"https://cloud.tencent.com/developer/techpedia/1929","sources":[2],"text":"性能测试"},{"ext":{"categoryId":1024,"categoryName":"通用技术 - 云原生","desc":"容器是一种轻量级的虚拟化技术,它允许将应用程序及其依赖项打包在一起,形成一个独立、可移植的运行环境。","kpCount":11,"name":"容器","pCategoryId":1002,"termId":1532},"id":442,"link":"https://cloud.tencent.com/product/tke","sources":[1,2],"text":"容器"},{"ext":{"categoryId":1018,"categoryName":"通用技术 - 编程语言","desc":"Java是一种计算机编程语言和平台,最初由Sun Microsystems于1995年发布。它是一种面向对象的语言,具有自动内存管理和垃圾回收功能,同时具有跨平台性,可以在不同的操作系统上运行。Java平台包括Java虚拟机(JVM)、Java API和Java编译器等组件,其中JVM是Java平台的核心,它可以将Java程序编译成字节码,然后在不同的操作系统上运行。Java具有简单、安全、可靠、可移植、高效等特点,被广泛应用于企业级应用开发、Web开发、游戏开发、移动应用开发等领域。","kpCount":8,"name":"Java","pCategoryId":1002,"termId":1644},"id":3376,"link":"https://cloud.tencent.com/developer/techpedia/1644","sources":[2],"text":"Java"},{"ext":{"categoryId":1020,"categoryName":"通用技术 - 云计算","desc":"API是Application Programming Interface的英文缩写,指的是应用程序接口。API定义了软件系统中的组件之间的交互方式,允许不同的软件之间进行交互和通信。它是一组规定的约定和协议,用于定义软件中不同模块和组件之间的通信方式。","kpCount":6,"name":"API","pCategoryId":1002,"termId":1539},"id":3297,"link":"https://cloud.tencent.com/developer/techpedia/1539","sources":[2],"text":"API"},{"ext":{"categoryId":1018,"categoryName":"通用技术 - 编程语言","desc":"CSS(层叠样式表)是一种用于描述HTML(超文本标记语言)和XML(可扩展标记语言)等文档外观和样式的语言。它可以控制网页中的字体、颜色、布局、边框、背景等方面的样式。CSS通过将样式与HTML文档分离,使网页的样式和布局更加灵活、可维护和可扩展。CSS可以在网页中内嵌、链接或直接写入HTML标记中。","kpCount":7,"name":"CSS","pCategoryId":1002,"termId":2043},"id":3738,"link":"https://cloud.tencent.com/developer/techpedia/2043","sources":[2],"text":"CSS"}]},"#url:\"/api/tag/products\",params:#tagIds:@18094,,objectType:1,objectId:2471045,,":[{"adActivity":{"id":5738,"lightSpotLabel":"HOT","pageUrl":"https://cloud.tencent.com/act/pro/double11-2024","priority":1,"startTime":"2023/12/12 17:58:46","title":"11.11 上云拼团GO"},"cnName":"容器服务","desc":"腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。","docURL":"https://cloud.tencent.com/document/product/457","hasActivity":false,"icon":"https://main.qcloudimg.com/image/product/2006/32_32/blue.svg","introURL":"https://cloud.tencent.com/product/tke","name":"tke","productId":10353,"shortDesc":"稳定、安全、高效、灵活扩展、简单易用的 Kubernetes 容器管理平台","tagId":10353}]},"tdk":{"title":"面试真题:Integer(128)引发的思考-腾讯云开发者社区-腾讯云","keywords":"腾讯技术创作特训营S10","description":"Hey小伙伴们,今天要给大家安利一篇操作实践的文章,相信大家通过仔细阅读,一定会有所收货!"},"meta":{"subject":"其他-空类-腾讯技术创作特训营S10","subjectTime":"2024-11-26 09:21:07","articleSource":"O","magicSource":"N","authorType":"G,O","productSlug":"tke"},"link":{"canonical":"https://cloud.tencent.com/developer/article/2471045"},"cssName":["Article","DraftMaster","Player"],"rbConfigKeys":["groupQRKeywords"],"pvId":"NJoiE2wksNiASnBpxZAc4","clientIp":"8.222.208.146","globalAnnounce":{"announceId":29,"content":"发文进行双十一晒单,聊省钱攻略,开箱云产品,最高赢万元奖励。点击查看活动详情:\u003ca href=\"https://cloud.tencent.com/developer/article/2462518\" target=\"_blank\"\u003ehttps://cloud.tencent.com/developer/article/2462518\u003c/a\u003e\u003cbr/\u003e \n \u003cimg src=\"https://qcloudimg.tencent-cloud.cn/raw/9f72dee064adec96fefa6f314527b6fe.jpg\"/\u003e","title":" 摇人!全网寻技术圈双十一课代表!"},"rbConfig":{"groupQRKeywords":{"AI":{"keywords":[],"img":"https://qcloudimg.tencent-cloud.cn/raw/89b22f53dc3d4e0516d0a4f74ab01a30.png"}},"versionUpdateTipList":"","navList":[{"text":"学习","menuList":[{"iconName":"article","title":"文章","desc":"技术干货聚集地","href":"/developer/column?from=19154"},{"iconName":"ask","title":"问答","desc":"技术问题讨论区","href":"/developer/ask?from=19155"},{"iconName":"video","title":"视频","desc":"技术视频记录区","href":"/developer/video?from=19156"},{"iconName":"learn","title":"学习中心","desc":"一站式学习平台","href":"/developer/learning"},{"iconName":"lab","title":"腾讯云实验室","desc":"体验腾讯云产品功能","href":"/lab/labslist?from=20154\u0026from_column=20154\u0026channel=c1004\u0026sceneCode=dev"}]},{"text":"活动","menuList":[{"iconName":"living","title":"直播","desc":"技术大咖面对面","href":"/developer/salon?from=19161"},{"iconName":"competition","title":"竞赛","desc":"秀出你的技术影响力","href":"/developer/competition?from=19162"}]},{"text":"专区","menuList":[{"iconName":"code","title":"CODING DevOps专区","desc":"云原生开发技术实践交流","href":"/developer/zone/codingdevops?from=19163"},{"iconName":"cloudstudio","title":"Cloud Studio专区","desc":"云端IDE开发者平台","href":"/developer/zone/cloudstudio?from=19165"},{"iconName":"cloudnative","title":"腾讯云原生专区","desc":"助力业务降本增效","href":"/developer/zone/cloudnative?from=19164"},{"iconName":"https://qccommunity.qcloudimg.com/community/image/ipass.svg","title":"腾讯轻联专区 ","desc":"新一代应用与数据集成平台","href":"/developer/zone/ipaas"},{"iconName":"https://qcloudimg.tencent-cloud.cn/raw/1deae15bfe2dcdd1036f601852df7dd2.svg","title":"腾讯云数据库专区","desc":"数据智能管理专家","href":"/developer/zone/tencentdb"},{"iconName":"https://qcloudimg.tencent-cloud.cn/raw/cb0703ff9de6b664413f4ed7d31a9224.svg","title":"腾讯云大数据专区","desc":"打造专业、易用、可信赖的大数据产品","href":"/developer/zone/bigdata"},{"iconName":"https://qcloudimg.tencent-cloud.cn/raw/1d60f881ef280ea992e2e4b6490d974b.svg","title":"腾讯云TCE专区","desc":"私有化云解决方案","href":"/developer/zone/tce"},{"iconName":"https://qccommunity.qcloudimg.com/community/image/cos.svg","title":"腾讯云存储专区","desc":"安全稳定的海量分布式存储服务","href":"/developer/zone/cos"},{"iconName":"https://qccommunity.qcloudimg.com/community/image/andon.svg","title":"腾讯云安灯专区","desc":"助力企业构建IT服务管理体系","href":"/developer/zone/tcandon"},{"iconName":"https://qccommunity.qcloudimg.com/community/image/sale.svg","title":"腾讯云运维技术沙龙","desc":"聚焦运维技术与云上治理","href":"/developer/zone/tcdevopssalon"},{"iconName":"https://qccommunity.qcloudimg.com/community/image/audio.svg","title":"腾讯云音视频","desc":"提供坚实的数字化助力","href":"/developer/zone/mediaservices"},{"iconName":"https://qccommunity.qcloudimg.com/icons/tencentcloudsmartadvisor.svg","title":"腾讯云顾问","desc":"实现便捷、灵活的一站式云上治理","href":"https://cloud.tencent.com/developer/zone/tencentcloudsmartadvisor"},{"iconName":"https://qccommunity.qcloudimg.com/icons/tencenthunyuan.svg","title":"腾讯混元","desc":"具备强大的中文创作、逻辑推理、任务执行能力","href":"https://cloud.tencent.com/developer/zone/tencenthunyuan"},{"iconName":"https://qccommunity.qcloudimg.com/community/image/ai.svg","title":"腾讯云智能","desc":"数实融合,云上智能","href":"https://cloud.tencent.com/developer/zone/ai"},{"iconName":"https://qccommunity.qcloudimg.com/community/image/Dnspod.svg","title":"dnspod","desc":"专业的智能DNS解析和域名托管服","href":"https://cloud.tencent.com/developer/zone/dnspod"},{"iconName":"https://qccommunity.qcloudimg.com/community/image/lighthouse.svg","title":"轻量应用服务器","desc":"新一代开箱即用、面向轻量应用场景的云服务器","href":"https://cloud.tencent.com/developer/zone/lighthouse"},{"iconName":"https://qccommunity.qcloudimg.com/community/image/HAi.svg","title":"高性能应用服务","desc":"提供即插即用的高性能云服务","href":"https://cloud.tencent.com/developer/zone/hai"},{"iconName":"https://qccommunity.qcloudimg.com/community/image/blockchain.svg","title":"腾讯云区块链","desc":"云链聚未来,协同无边界","href":"https://cloud.tencent.com/developer/zone/tencentcouldbockchain"},{"iconName":"https://qccommunity.qcloudimg.com/community/image/OpencloudOS.svg","title":"OpenCloudOS","desc":"开源中立的国产操作系统社区","href":"https://cloud.tencent.com/developer/zone/opencloudos"},{"iconName":"https://cloudcache.tencent-cloud.com/qcloud/ui/static/static_source_business/b3e1b483-be77-4e08-827f-ef0e5cda26cf.svg","title":"边缘安全加速平台EO","desc":"下一代CDN—EdgeOne,不止加速","href":"https://cloud.tencent.com/developer/zone/tencentcloudedgeone"}]},{"text":"工具","menuList":[{"iconName":"https://qccommunity.qcloudimg.com/icons/ai-assistant.svg","title":"腾讯云AI代码助手","desc":"辅助编码工具,使研发提效增质","href":"https://cloud.tencent.com/product/acc?from=22178"},{"iconName":"coding","title":"CODING DevOps","desc":"一站式软件研发管理平台","href":"/product/coding?from=20154\u0026from_column=20154"},{"iconName":"studio","title":"Cloud Studio","desc":"随时随地在线协作开发","href":"/product/cloudstudio?from=20154\u0026from_column=20154"},{"iconName":"sdk","title":"SDK中心","desc":"开发者语言与SDK","href":"/document/sdk?from=20154\u0026from_column=20154"},{"iconName":"api","title":"API中心","desc":"API 助力快捷使用云产品","href":"/document/api?from=20154\u0026from_column=20154"},{"iconName":"tool","title":"命令行工具","desc":"可快速调用管理云资源","href":"/document/product/440/6176?from=20154\u0026from_column=20154"}]}],"activity-popup":{"mImgUrl":"https://qccommunity.qcloudimg.com/mp/images/11-11mobile.jpg","imgUrl":"https://qccommunity.qcloudimg.com/mp/images/11-11pc.jpg","beginTime":"2024/10/24 00:00:00","endTime":"2024/10/31 23:59:59"},"header-advertisement":{"imageUrl":"https://qccommunity.qcloudimg.com/image/2024-11-01-18-15.png","link":"https://cloud.tencent.com/act/pro/double11-2024?from=22374\u0026from_column=22374#miaosha"}},"isBot":false,"session":{"isLogined":false,"isQCloudLogined":false,"isQCommunityLogined":false,"isDifferentUin":false}}},"page":"/article/[articleId]","query":{"policyId":"20240000","traceId":"","articleId":"2471045"},"buildId":"5MdUrwfvVsazh9zbVIX0r","assetPrefix":"https://qccommunity.qcloudimg.com/community","isFallback":false,"gssp":true,"appGip":true,"scriptLoader":[]}</script></body></html>

Pages: 1 2 3 4 5 6 7 8 9 10